1

I have this code:

wcout << jvalue.serialize() << endl;

which prints out the whole body of json data:

{
    "data": {
        "timestamp": [
            {
                "_id": "5ad93fc48084e1089cd9667b",
                "staff_id": 172,
                "staff_name": "Michael Edano",
                "time_in": "2018-04-20T01:17:56.787Z",
                "time_out": null,
                "status": ['Leave','Vacation','Absent'],
                "createdAt": "2018-04-20T01:17:56.790Z",
                "updatedAt": "2018-04-20T01:17:56.790Z",
                "__v": 0
            }
        ],
        "success": true
    }
}

Can someone give me an example on how to get let say the _id field and single data of status[] field (which is array)

from data return by web::json::value::serialize() ?

Thank you.

noyruto88
  • 767
  • 2
  • 18
  • 40

1 Answers1

2

You don't have to call serialize to access the json values. Once you have a json::value, which holds a json object, you can traverse it to obtain inner objects and arrays as json::value's:

json::value jvalue; //this is your initial value

// this new value will hold the whole 'data' object:
json::value data = jvalue.at(U("data")); 

// read `timestamp` array from `data` and directly read 
// the item at index 0 from it:
json::value timestamp = data.at(U("timestamp")).at(0);

// from `timestamp` (first item of timestmap array`)
json::value id = timestamp.at(U("_id"));

// print `id` as string
std::wcout << id.as_string() << std::endl;

// read `status` array first item and print it as string
json::value status = timestamp.at(U("status"));
std::wcout << status.at(0).as_string() << std::endl;

As you can guess from the code above, calls to at can be chained together:

// read the `_id` value at once and print it as string
std::wcout << jvalue.at(U("data")).at(U("timestamp")).at(0).at(U("_id")).as_string() << std::endl;

Everything is well explained here.

p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35
  • Hi sir Paolo, Thank you. I guess this should supposed to work but when I tried to run the program the console appears without output and the json.h files were opened to ide, with some message "Unhandled exception at 0x00007FF8B7C34008 in MathClient.exe: Microsoft C++ exception: web::json::json_exception at memory location 0x00000001000FF3C0." – noyruto88 Apr 20 '18 at 07:53
  • Here's my full code: https://github.com/Microsoft/cpprestsdk/issues/739 Thank you. – noyruto88 Apr 20 '18 at 08:13
  • You have an **unhandled** exception. Enclose the whole code in a `try` block and try to catch the exception with a `catch(const std::exception &e)` to see what went wrong. – p-a-o-l-o Apr 20 '18 at 08:19
  • I tried but there's no error show, and there's no output also. – noyruto88 Apr 20 '18 at 08:41
  • Have a `std::cout << e.what() << std::endl;` inside the `catch` block. – p-a-o-l-o Apr 20 '18 at 08:43
  • Yes, I did that. And by the way, when the json.h file of cpprest automatically opened, it points this line with code virtual json::object& as_object() { throw json_exception(_XPLATSTR("not an object")); }. Actually this only happens when I inserted your code answer. – noyruto88 Apr 20 '18 at 09:09
  • I updated my code in github, please see if my try catch is correct. https://github.com/Microsoft/cpprestsdk/issues/739 thank you. – noyruto88 Apr 20 '18 at 09:15
  • @noyruto88 in your `main` you're passing a json value to the `display_json` function, which has nothing to do with the code I gave you. That code is supposed to read from the data you posted. Get rid of the `display_json` in `main` and see what happens. – p-a-o-l-o Apr 20 '18 at 09:20
  • I used the display_json() to display returned json into console. like when i displayed before wcout << jvalue.serialize() << endl; . Sorry about that. – noyruto88 Apr 20 '18 at 09:25
  • It display now in the console that says "not an object". It seems i miss used your code. Can you tell me where should i put it? Thank you. – noyruto88 Apr 20 '18 at 09:34
  • @noyruto88 It's ok to keep it there, since the passed in json is **exactly** the one you posted above. Otherwise it could work or not. Keep a line `wcout << jvalue.serialize() << endl;` at the very beginning of the `display_json` function, and comment my code. Check what json you're getting from the http response, then adjust my code to read it properly. Right? – p-a-o-l-o Apr 20 '18 at 09:51
  • I updated my code and It has output now. https://github.com/Microsoft/cpprestsdk/issues/739 What display_json() doing is processing the "172" json data that i pass which is not an object and process second the value of jvalue which have data from api. It's kinda funny. Haha. Anyways, Thank you very much. – noyruto88 Apr 20 '18 at 10:08
  • So my answer didn't help? :( – p-a-o-l-o Apr 20 '18 at 12:49
  • Its a great help. Thanks a lot. – noyruto88 Apr 20 '18 at 13:39
  • Hi sir Paolo, do you have an idea about this? https://stackoverflow.com/questions/50096040/how-to-iterate-through-json-objects-with-cpprest Thank you. – noyruto88 Apr 30 '18 at 07:45
  • @p-a-o-l-o Any idea why when I debug this line web::json::value jsonValue = httpResponse.extract_json().get();, If I watch for jsonValue.is_null(), it shows web::json::value::is_null has no address, possibly due to compiler optimizations? – pixel Sep 24 '20 at 00:29
  • Why is call to serialize() needed? I am just curious. When should I call serialize()? Thanks – pixel Oct 05 '20 at 20:06