microsoft / cpprestsdk

The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
Other
7.96k stars 1.65k forks source link

Handle JSON Responses #1672

Closed dedetuga closed 2 years ago

dedetuga commented 2 years ago

Hi,

I'm trying cpprestsdk with json responses for the first time. I tested Getting Started Tutorial and try understand JSON but with no success, I don't understand how do i get data out of Json responses and handle it.

Sample server Rest API Response: {"error":[],"result":{"1INCH":{"aclass":"currency","altname":"1INCH","decimals":10,"display_decimals":5},"AAVE":{"aclass":"currency","altname":"AAVE","decimals":10,"display_decimals":5},"ADA":{"aclass":"currency","altname":"ADA","decimals":8,"display_decimals":6},"ADA.S":{"aclass":"currency","altname":"ADA.S","decimals":8,"display_decimals":6},"ALGO":{"aclass":"currency","altname":"ALGO","decimals":8,"display_decimals":5},"ALGO.S":{"aclass":"currency","altname":"ALGO.S","decimals":8,"display_decimals":5}}}

Some help? Thanks

adrian-afloarei commented 2 years ago

They have json.h that handles json parsing, validation, serialization, ... For starters, check out web::json::value::parse(data) where 'data' is the response string that you provided or any valid json.

This is the definition of the parse method from json.h:

/// <summary>
/// Parses a string and construct a JSON value.
/// </summary>
/// <param name="value">The C++ value to create a JSON value from, a C++ STL string of the
/// platform-native character width</param>
_ASYNCRTIMP static value __cdecl parse(const utility::string_t& value);