C语言 json数组在c中解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16975918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
json array parsing in c
提问by lenin T.mohan
i am trying to parse a json array,i am facing problem.
我正在尝试解析一个 json 数组,我遇到了问题。
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?
我能够访问药物数组并打印总数组,但无法访问数组内的对象。你能建议任何解决方案或任何例子来使用 C 语言来做到这一点吗?
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.
问候,列宁。
回答by Aiias
You need to access the inner array using a json_object *variable.
您需要使用json_object *变量访问内部数组。
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));
}
回答by spmno
You can use the jsoncpp to do this job. Array as a Json::Value, you can
您可以使用 jsoncpp 来完成这项工作。数组作为 Json::Value,你可以
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.
我希望能帮到你。
回答by leonhart
If you want to access the array as a json object, you should get it as a json_object* but not a array_list*
如果要将数组作为 json 对象访问,则应将其作为 json_object* 而不是 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");

