0

I'm doing a rest api and i using the cJSON c library in c++.

This is my body request example

{
  "userEmail": "email@email.com",
  "userPassword": "12345678"
}

In my c++ program i receive this json like this (its work now):

cJSON *root;

root = cJSON_CreateObject();

cJSON_AddStringToObject(root, "userEmail", userEmail.c_str());
cJSON_AddStringToObject(root, "userPassword", userPassword.c_str());

Now i need to change my body request to something like that:

{
  "userInfo":{
      "userEmail": "email@email.com",
      "userPassword": "12345678"
  }
}

Note: It is not a array, its like a json 'section'. I dont find any solution to get the content inside "userInfo" (mail and password) using the cJSON library. Can you help me?

Thanks a lot

Pik93
  • 49
  • 1
  • 2
  • 12
  • I would recommand https://github.com/nlohmann/json for "JSON in modern C++" . It' header only, easy to use and the development is very active. – Andrei Damian Apr 04 '17 at 11:22

1 Answers1

1
cJSON *root;
cJSON *info;

root = cJSON_CreateObject();

cJSON_AddItemToObject(root, "userInfo", info = cJSON_CreateObject());
cJSON_AddStringToObject(info, "userEmail", userEmail.c_str());
cJSON_AddStringToObject(info, "userPassword", userPassword.c_str());

cJSON Github Repository

sharyex
  • 460
  • 1
  • 5
  • 17
  • Hi. thanks for the comment. It helps me. But i forget to write one thing. I have a function that receive 2 string, the userEmail and userPassword.. so now what parameters the function need to receive? because now the body is not only the email and passord.. it is the user info... – Pik93 Apr 04 '17 at 10:58
  • You should be able to use the same params, the `userInfo` is just a wrapper object, unless you mean how to parse the object and get the values inside? – sharyex Apr 04 '17 at 11:06
  • My function receive the email and password and i can parse de values and show.. but now with the userInfo its not working, my request body its null – Pik93 Apr 04 '17 at 12:14
  • There are examples on the repository I linked on how to parse the children of an object. If you still can't figure it out, make a new post, can't help you without seeing the full code. – sharyex Apr 04 '17 at 12:17
  • I cannot post all code, its very complex and big. My client it is in java and send json data from my java client to my c++ server. In server i parse the data, do the things i need you data and send back to my java client the reply response – Pik93 Apr 04 '17 at 12:19