1

I have this body request example:

{
   "users": [{
        "userId": 123
    }, {
        "userId": 1234
    }]
}

For the previous example I receive one std::list<UsersId>* VUsers that have my userId (in this case '123' and '1234'), create cJSON array, iterate my list and get all userId. (Note: the UsersId is one auxiliar class that I use and receive one int in constructor)

cJSON* cJsonUsers = cJSON_CreateArray();
cJSON_AddItemToObject(root, "VUsers", cJsonUsers);

    std::list<UsersId>::const_iterator itUsers = VUsers->begin();
    while (itUsers != VUsers->end())
    {
        cJSON *cJsonVNode = cJSON_CreateObject();
        cJSON_AddItemToArray(cJsonUsers, cJsonUser);

        cJSON_AddNumberToObject(cJsonUser, "userId", itUsers->userId);
        ++itVNodes;
    }

But know I want to the same but make more simple/easy and need to change the body request to something like this:

{
    "users": {
        "userId": [123, 1234]
    }
}

I'm using this c++ library -> https://github.com/DaveGamble/cJSON but I dont understand how to do to implement the modification that I need.

EDIT 2 (PARSE THE JSON)

cJSON* cJsonUsers = cJSON_GetObjectItem(root, "users");
if (!cJsonUsers) return 0;
if (cJsonUsers->type != cJSON_Array) return 0;

std::list<VUserId>* users = new std::list<VUserId>();
cJSON* cJsonVUser;
cJSON_ArrayForEach(cJsonVUser, cJsonUsers)
{
    cJSON* cJsonVUserId = cJSON_GetObjectItem(cJsonVUser, "userId");
    if (!cJsonVUserId) continue;

int user_id = cJsonVUserId->valueint;

VUserId userId(user_id);
users->push_back(userId);
}
Sled
  • 18,541
  • 27
  • 119
  • 168
Pik93
  • 49
  • 1
  • 2
  • 12
  • Please try to be more precise. *"I dont understand how to do to implement the modification"* is not a sufficient problem description. Does it not compile? If not, which line fails and what's the error message? Or does it not produce the correct output? If not, what's the test input, what's the expected output and what's the actual output? – Christian Hackl Apr 07 '17 at 15:23
  • I understand the code (that the other user put) and i implement this part. Now i have other part (edit 2 is the code that work after the body request modification) where i need to parse the json. So i need go to the users object, and get all values inside the usersId array (probably with some iteration) and save all values in other list ( std::list* users = new std::list(); ) – Pik93 Apr 07 '17 at 15:37
  • From what I see in part 2, two variables are defined with the same name (userId). – Ghislain Fourny Apr 07 '17 at 15:38
  • my mistake. i already modify the example code that i copy to here. Thanks – Pik93 Apr 07 '17 at 15:40
  • Now using you code, who its possible to do the json parse like i do before in edit 2? – Pik93 Apr 07 '17 at 15:42

1 Answers1

0

Something like this could work, that is, create the object and array outside of the loop, and insert the numbers inside the loop:

cJSON* cJsonUsers = cJSON_CreateObject();
cJSON_AddItemToObject(root, "users", cJsonUsers);

cJSON* cJsonUserId = cJSON_CreateArray();
cJSON_AddItemToObject(cJsonUsers, "userId", cJsonUserId);

std::list<UsersId>::const_iterator itUsers = VUsers->begin();
while (itUsers != VUsers->end())
{
    cJSON_AddItemToArray(cJsonUserId, cJSON_CreateNumber(itUsers->userId));
    ++itVNodes;
}

Note that there are languages out there that are more convenient to manipulate JSON if you have the flexibility (disclaimer: I was involved in the design of some of these). Of course there are always use cases when you have to use C++ and in which a library makes a lot of sense.

With languages such as C++ or Java, there is an impedance mismatch between objects in the classical sense, and data formats like XML or JSON. For example, with the standardized, declarative and functional XQuery 3.1 this does not need much code to transform the first document into the second:

let $original-document := json-doc("users.json")
return map {
  "users" : map {
    "userId" : array { $original-document?users?*?userId }
  }
}
Community
  • 1
  • 1
Ghislain Fourny
  • 6,971
  • 1
  • 30
  • 37
  • HI. thanks for the reply. I know but in this case my server must be in c++. Please see my EDIT 2 in original post. I put the part to do the json parse, try to modify this part too but its not working. This part is the original code that work on previous body request after do the changes – Pik93 Apr 07 '17 at 15:17
  • What "impedance mismatch" would that be, exactly? Are you referring to the fact that pointers cannot be expressed via JSON or XML? – Christian Hackl Apr 07 '17 at 15:20
  • @ChristianHackl this is part of it, but it is also that Java or C++ are object-oriented imperative languages with general capabilities while declarative querying languages are more adequate to manipulate data and syntax in general. SQL is the most famous example, for tabular data, and most C++ users do use SQL when connecting to a relational database rather than write low-level code that uses STL iterators on tables in memory. This has not yet established itself for tree-like data, but the idea is similar. The most factual argument is, I think, the lines of code needed, as well as readability. – Ghislain Fourny Apr 07 '17 at 15:29