12

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

My array is like this:

configure: {
  "id": 4,
  "userId": 107,
  "deviceMacAddress": "00:06:66:30:02:3C",
  "medication": [{
    "id": 11,
    "version": 18,
    "name": "name1",
    "unit": "mg",
    "forMed": "for1",
    "schedule": [1]
  }, {
  "id": 45,
  "version": 1,
  "name": "sdga",,
  "unit": "mg",
  "forMed": "54234",
  "schedule": [0,1,2,3,4,5,6]
  }],

i am able to access medication array and print total array,but not able to access objects inside array. can you pls suggest any solution or any example to do this using C language?

MyCode

int main(int argc, char **argv) {
  struct json_object *med_obj, *medi_obj, *tmp1_obj;
  struct array_list *lArray;
  charname[10] = {0};
  static const char filename[] = "xyz.txt";
  med_obj = json_object_from_file(filename);
  medi_obj = json_object_object_get(med_obj, "medication");
  lArray = json_object_get_array(medi_obj);
  tmp1_obj = json_object_object_get(medi_obj, "name");
  strcpy (name,json_object_to_json_string(tmp1_obj));
  printf("name=%s\n",name);
}

Regards, Lenin.

6
  • Can we see what you have coded so far to help advise you on where to move forward? Commented Jun 7, 2013 at 3:49
  • 4
    That's not valid JSON. Commented Jun 7, 2013 at 3:49
  • MyCode: int main(int argc, char *argv){ struct json_objectmed_obj,*medi_obj,*tmp1_obj; struct array_list *lArray; charname[10]={0}; static const char filename[] = "xyz.txt"; med_obj=json_object_from_file(filename); medi_obj = json_object_object_get(med_obj, "medication"); lArray=json_object_get_array(medi_obj); tmp1_obj = json_object_object_get(medi_obj, "name"); strcpy (name,json_object_to_json_string(tmp1_obj)); printf("name=%s\n",name); Commented Jun 7, 2013 at 4:05
  • Hi i have put my code, can you please check and suggest any solution? Commented Jun 7, 2013 at 4:05
  • @Aiias i have put my code,can you pls check and suggest any solution. Commented Jun 7, 2013 at 4:55

3 Answers 3

21

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));
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks aiias,it's working.I am facing new issue whenever trying to access an object in array(eg:name in my case)all the values with name are printing.I want only particular values.can you pls suggest any solution.Once again thanks for the help.
Hi @Aiias while printing an object (eg:name in my array)iam getting all the values with name,i want to print only certain values with name tag. can you pls suggest any solution
@leninT.mohan - What do you mean by you are getting all the values with name? Currently we are looping through the array. If you only want the first object in the array, don't use a for loop and instead just do medi_array_obj = json_object_array_get_idx(medi_array, 0);
Hi @Aiias iwant all the object values but i have to print each value in different line(i.e "name" =1 in line1,next occurence of object "name" in array in second line and so on upto 10 times).Now when i am printing they are printing in a single line.How can i read all the objects in same way and print them?pls check my code in my earlier posts. Thanks for you reply
@leninT.mohan - Are you saying that printf("name = %s \n", json_object_get_string(medi_array_obj_name)); does not print out multiple lines? The "\n" newline character should break to the next line. Try adding a space between the = and then \n.
|
0

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.

Comments

0

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.