Using cJSON to read in a JSON array

后端 未结 4 767
醉梦人生
醉梦人生 2021-02-04 09:42

I am attempting to use the cJSON library, written by Dave Gamble, to read in the following JSON array:

\"items\": 
[
    {
        \"name\": \"command\",
                


        
4条回答
  •  自闭症患者
    2021-02-04 10:13

    My guess (not having read the spec, and being a bit rusty with C):

    request_json = cJSON_Parse(request_body);
    
    items = cJSON_GetObjectItem(request_json, "items");
    for (int i = 0; i < max; i++) {  // Presumably "max" can be derived from "items" somehow
    
        cJSON* item = cJSON_GetArrayItem(items, i);
    
        name = cJSON_GetObjectItem(item, "name");
        index = cJSON_GetObjectItem(item, "index");
        optional = cJSON_GetObjectItem(item, "optional");
    
        // Stash above info somewhere
    }
    

提交回复
热议问题