2

I have a question about the C parse the json array, I know how cJSON parse the json use C, but I can't find any result in Google about how to parse the json array, I have watched the Using cJSON to read in a JSON array, but it doesn't suit me.

I recive a json array from web API and it look like this:

[{\"id\":\"25139\",\"date\":\"2016-10-27\",\"name\":\"Komfy Switch With Camera DKZ-201S\\/W Password Disclosure\"},{\"id\":\"25117\",\"date\":\"2016-10-24\",\"name\":\"NETDOIT weak password Vulnerability\"}]

As you see, there are many json in a array, so, how can i parse the array with cJSON lib?

4
  • Your sample is not valid JSON. Basically, all he backslashes are invalid. Is this an artifcat from copying it from a debugger, which shows strings with C-style escaping? Commented Nov 7, 2016 at 8:31
  • yes, you r right, i copy it from gdb, and when i use the curl to download the data, it's have no "\", it's like this: Commented Nov 7, 2016 at 8:37
  • [{"id":"6792","date":"2010-01-29","name":"Discuz! 6.0.0 cross site scripting"},{"id":"7570","date":"2009-09-17","name":"Discuz! Plugin Crazy Star <= 2.0 (fmid) SQL Injection Vulnerability"},{"id":"7619","date":"2009-09-15","name":"Discuz! JiangHu plugin versions 1.1 and below remote SQL injection"},{"id":"7779","date":"2009-08-25","name":"Discuz 6.0 (2fly_gift.php) Sql Injection Vulnerability"},{"id":"7878","date":"2009-08-19","name":"Discuz! Remote Reset User Password Exploit"}] Commented Nov 7, 2016 at 8:37
  • I hope this may help you, github.com/ajithcofficial/ajson Commented Feb 24, 2020 at 9:59

1 Answer 1

9

cJSON supports the full range, i.e. both JSON arrays and objects. When accessing the data, you just need to understand what the type of the current piece is.

In your case, it's an array containing objects containg simple values. So this is how you handle it:

int i;
cJSON *elem;
cJSON *name;
char *json_string = "[{\"id\":\"25139\",\"date\":\"2016-10-27\",\"name\":\"Komfy Switch With Camera DKZ-201S\\/W Password Disclosure\"},{\"id\":\"25117\",\"date\":\"2016-10-24\",\"name\":\"NETDOIT weak password Vulnerability\"}]";
cJSON *root = cJSON_Parse(my_json_string);
int n = cJSON_GetArraySize(root);
for (i = 0; i < n; i++) {
    elem = cJSON_GetArrayItem(root, i);
    name = cJSON_GetObjectItem(elem, "name");
    printf("%s\n", name->valuestring);
}

I haven't compiled it. I hope it's not too far off.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, your solution make me suddenly enlightened and then instantly understand, very grateful.
Note: this has O(n^2) behaviour(one N for the loop, one N inside cJSON_GetArrayItem), might try cJSON_ArrayForEach(), as mentioned in the readme.

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.