Using cJSON to read in a JSON array

后端 未结 4 723
醉梦人生
醉梦人生 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:17

    Document mentions about parse_object().

    I think this is what you need to do.

    void parse_object(cJSON *root)
    {
      cJSON* name = NULL;
      cJSON* index = NULL;
      cJSON* optional = NULL;
    
      int i;
    
      cJSON *item = cJSON_GetObjectItem(items,"items");
      for (i = 0 ; i < cJSON_GetArraySize(item) ; i++)
      {
         cJSON * subitem = cJSON_GetArrayItem(item, i);
         name = cJSON_GetObjectItem(subitem, "name");
         index = cJSON_GetObjectItem(subitem, "index");
         optional = cJSON_GetObjectItem(subitem, "optional"); 
      }
    }
    

    Call this function as

    request_json = cJSON_Parse(request_body);
    parse_object(request_json);
    

提交回复
热议问题