Project

General

Profile

simpler way to access a Json variable

Added by Bruno Zerbo almost 5 years ago

I have a Json string that I have successfully parsed to get the variable value in this manner:

            std::string stringwithJson = "....Json code..."
            Json::Object result;
            Json::parse(stringwithJson, result);

//I'm trying to get the variable inside result::routes[0]::legs[0]::steps[2]::distance::value 
            Json::Array routes = result["routes"];
            Json::Array legs = static_cast<Json::Object>(routes.at(0)).get("legs");
            Json::Array steps = static_cast<Json::Object>(legs.at(0)).get("steps");
            Json::Object steps_2= steps.at(2);
            Json::Object distance = steps_2["distance"];
            int value = distance["value"];
            std::cout << "Distance value = " << value << std::endl;

I'm writing 6 lines of code to get my variable. Is there a simpler way?

It would be beautiful to may write somethings like result.get("routes").at(0).get("legs").at(0).get("steps").at(2).get("distance").value but we can't due to the necessity di cast.

Maybe It is the cost to pay to use c in place of , for example, .NET.

Thanks for reply


Replies (4)

RE: simpler way to access a Json variable - Added by Bruno Zerbo almost 5 years ago

My Json code is

{
   "geocoded_waypoints" : [
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJzeQPphVYERMRYEK4ZykECwQ",
         "types" : [ "administrative_area_level_3", "political" ]
      },
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJ4UgtC5r7ExMRPI9qYCPgIj8",
         "types" : [ "locality", "political" ]
      }
   ],
   "routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : 37.555756,
               "lng" : 15.1449201
            },
            "southwest" : {
               "lat" : 37.5205644,
               "lng" : 15.020874
            }
         },
         "copyrights" : "Map data ©2019",
         "legs" : [
            {
               "distance" : {
                  "text" : "15.1 km",
                  "value" : 15102
               },
               "duration" : {
                  "text" : "27 mins",
                  "value" : 1645
               },
               "end_address" : "95021 Aci Castello, Province of Catania, Italy",
               "end_location" : {
                  "lat" : 37.555756,
                  "lng" : 15.1444998
               },
               "start_address" : "95045 Misterbianco, Province of Catania, Italy",
               "start_location" : {
                  "lat" : 37.5292177,
                  "lng" : 15.020874
               },
               "steps" : [
                  {
                     "distance" : {
                        "text" : "0.3 km",
                        "value" : 307
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 55
                     },
                     "end_location" : {
                        "lat" : 37.5278512,
                        "lng" : 15.0226143
                     },
                     "html_instructions" : "Head \u003cb\u003esoutheast\u003c/b\u003e on \u003cb\u003eVia della Campana\u003c/b\u003e toward \u003cb\u003eVia Mario Moschetti\u003c/b\u003e",
                     "polyline" : {
                        "points" : "s|`dFmwtzAHIJKRQPKn`_@FCFCDCJItA_BLOf@k@f@i@LQNU@C@A?C?AAC?CACAAAACACAC?E?C@QFa`P"
                     },
                     "start_location" : {
                        "lat" : 37.5292177,
                        "lng" : 15.020874
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "84 m",
                        "value" : 84
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 14
                     },
                     "end_location" : {
                        "lat" : 37.5273584,
                        "lng" : 15.0233117
                     },
                     "html_instructions" : "Sharp \u003cb\u003eright\u003c/b\u003e to stay on \u003cb\u003eVia della Campana\u003c/b\u003e",
                     "maneuver" : "turn-sharp-right",
                     "polyline" : {
                        "points" : "at`dFibuzA?G`C@C@E@C@AFIPSb@g@`CJKBGFQ"
                     },
                     "start_location" : {
                        "lat" : 37.5278512,
                        "lng" : 15.0226143
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "1.8 km",
                        "value" : 1785
                     },
                     "duration" : {
                        "text" : "4 mins",
                        "value" : 238
                     },
                     "end_location" : {
                        "lat" : 37.5368933,
                        "lng" : 15.0385343
                     },



etc etc

RE: simpler way to access a Json variable - Added by Roel Standaert almost 5 years ago

Why .NET as an example? C# is also a statically typed language.

Other JSON libraries do it differently, e.g. probably the most well-known one infers that you want to use it as an array if you use operator[] with an integer key, or as an object if you use operator[] with a string key.

Wt's JSON implementation is a very minimal implementation. Maybe we could consider supplanting it with another implementation rather than iterating on it. I don't consider Wt's JSON implementation a key feature of Wt.

In any case, you can always use another implementation if Wt's implementation is not to your liking.

RE: simpler way to access a Json variable - Added by Roel Standaert almost 5 years ago

Note: you're doing a lot of copies. You should be able to do a static_cast<Json::Object&> rather than static_cast<Json::Object>, and static_cast<Json::Array&> rather than static_cast<Json::Array>.

RE: simpler way to access a Json variable - Added by Bruno Zerbo almost 5 years ago

Thank you. I forgot to use a reference instead of the copy.

    (1-4/4)