json array parsing in c

前端 未结 3 511
深忆病人
深忆病人 2021-01-04 22:32

i am trying to parse a json array,i am facing problem.

My array is like this:

configure: {
  \"id\": 4,
  \"userId\": 107,
  \"deviceMacAddress\": \"         


        
相关标签:
3条回答
  • 2021-01-04 22:52

    You can use the jsoncpp to do this job. Array as a Json::Value, you can

    medicationValue = jsonObject[medicationKey];
    Json::Value::Members member;
    member = medicationValue .getMemberNames();
    
    for (Json::Value::Members::iterator iter = member.begin(); iter != member.end(); iter++) {
        the element of medication here
    }
    

    I hope will help you.

    0 讨论(0)
  • 2021-01-04 22:54

    If you want to access the array as a json object, you should get it as a json_object* but not a array_list*

    struct json_object *lArray; 
    ...
    lArray=json_object_get(medi_obj); 
    tmp1_obj = json_object_object_get(json_object_array_get_idx(lArray, 0), "name");
    
    0 讨论(0)
  • 2021-01-04 23:02

    You need to access the inner array using a json_object * variable.

    Try this:

    struct json_object *med_obj, *medi_array, *medi_array_obj, *medi_array_obj_name;
    int arraylen, i;
    charname[10] = {0};
    static const char filename[] = "xyz.txt";
    med_obj = json_object_from_file(filename);
    medi_array = json_object_object_get(med_obj, "medication");
    
    // medi_array is an array of objects
    arraylen = json_object_array_length(medi_array);
    
    for (i = 0; i < arraylen; i++) {
      // get the i-th object in medi_array
      medi_array_obj = json_object_array_get_idx(medi_array, i);
      // get the name attribute in the i-th object
      medi_array_obj_name = json_object_object_get(medi_array_obj, "name");
      // print out the name attribute
      printf("name=%s\n", json_object_get_string(medi_array_obj_name));
    }
    
    0 讨论(0)
提交回复
热议问题