1

I have the following JSON data returned to me from a server, and I'm unable to figure out how to parse the maneuvers array for the narrative:

{
"maneuvers": [
          {
            "signs": [],
            "index": 0,
            "maneuverNotes": [],
            "direction": 8,
            "narrative": "Start out going east on Clarendon Blvd toward N Queen St.",
            "iconUrl": "http://content.mapquest.com/mqsite/turnsigns/icon-dirs-start_sm.gif",
            "distance": 0.031,
            "time": 6,
            "linkIds": [],
            "streets": [
              "Clarendon Blvd"
            ],
            "attributes": 0,
            "transportMode": "AUTO",
            "formattedTime": "00:00:06",
            "directionName": "East",
            "mapUrl": "http://www.mapquestapi.com/staticmap/v4/getmap?key=KEY&type=map&size=225,160&pois=purple-1,38.893164999999996,-77.077957,0,0|purple-2,38.893276,-77.077407,0,0|&center=38.8932205,-77.077682&zoom=15&rand=-1645843521&session=55e60cd9-00b6-001a-02b7-20ac-00163e7dd551",
            "startPoint": {
              "lng": -77.077957,
              "lat": 38.893165
            },
            "turnType": 2
          },
          {
            "signs": [],
            "index": 1,
            "maneuverNotes": [],
            "direction": 4,
            "narrative": "Turn right onto N Queen St.",
            "iconUrl": "http://content.mapquest.com/mqsite/turnsigns/rs_right_sm.gif",
            "distance": 0.168,
            "time": 32,
            "linkIds": [],
            "streets": [
              "N Queen St"
            ],
            "attributes": 0,
            "transportMode": "AUTO",
            "formattedTime": "00:00:32",
            "directionName": "South",
            "mapUrl": "http://www.mapquestapi.com/staticmap/v4/getmap?key=KEY&type=map&size=225,160&pois=purple-2,38.893276,-77.077407,0,0|purple-3,38.890857,-77.07708699999999,0,0|&center=38.8920665,-77.077247&zoom=12&rand=-1645843521&session=55e60cd9-00b6-001a-02b7-20ac-00163e7dd551",
            "startPoint": {
              "lng": -77.077407,
              "lat": 38.893276
            },
            "turnType": 2
          },
          {
            "signs": [],
            "index": 2,
            "maneuverNotes": [],
            "direction": 7,
            "narrative": "Turn right onto 14th St N.",
            "iconUrl": "http://content.mapquest.com/mqsite/turnsigns/rs_right_sm.gif",
            "distance": 0.003,
            "time": 5,
            "linkIds": [],
            "streets": [
              "14th St N"
            ],
            "attributes": 0,
            "transportMode": "AUTO",
            "formattedTime": "00:00:05",
            "directionName": "West",
            "mapUrl": "http://www.mapquestapi.com/staticmap/v4/getmap?key=KEY&type=map&size=225,160&pois=purple-3,38.890857,-77.07708699999999,0,0|purple-4,38.890842,-77.077148,0,0|&center=38.8908495,-77.07711749999999&zoom=15&rand=-1646228270&session=55e60cd9-00b6-001a-02b7-20ac-00163e7dd551",
            "startPoint": {
              "lng": -77.077087,
              "lat": 38.890857
            },
            "turnType": 2
          },
          {
            "signs": [
              {
                "text": "50",
                "extraText": "",
                "direction": 7,
                "type": 2,
                "url": "http://icons.mqcdn.com/icons/rs2.png?n=50&d=WEST"
              }
            ],
            "index": 3,
            "maneuverNotes": [],
            "direction": 4,
            "narrative": "Merge onto Arlington Blvd/US-50 W via the ramp on the left.",
            "iconUrl": "http://content.mapquest.com/mqsite/turnsigns/rs_merge_left_sm.gif",
            "distance": 1.606,
            "time": 151,
            "linkIds": [],
            "streets": [
              "Arlington Blvd",
              "US-50 W"
            ],
            "attributes": 0,
            "transportMode": "AUTO",
            "formattedTime": "00:02:31",
            "directionName": "South",
            "mapUrl": "http://www.mapquestapi.com/staticmap/v4/getmap?key=KEY&type=map&size=225,160&pois=purple-4,38.890842,-77.077148,0,0|purple-5,38.87355,-77.089424,0,0|&center=38.882196,-77.08328599999999&zoom=9&rand=-1646228270&session=55e60cd9-00b6-001a-02b7-20ac-00163e7dd551",
            "startPoint": {
              "lng": -77.077148,
              "lat": 38.890842
            },
            "turnType": 11
          },
          {
            "signs": [],
            "index": 4,
            "maneuverNotes": [],
            "direction": 4,
            "narrative": "Turn left onto N Fillmore St.",
            "iconUrl": "http://content.mapquest.com/mqsite/turnsigns/rs_left_sm.gif",
            "distance": 0.408,
            "time": 74,
            "linkIds": [],
            "streets": [
              "N Fillmore St"
            ],
            "attributes": 0,
            "transportMode": "AUTO",
            "formattedTime": "00:01:14",
            "directionName": "South",
            "mapUrl": "http://www.mapquestapi.com/staticmap/v4/getmap?key=KEY&type=map&size=225,160&pois=purple-5,38.87355,-77.089424,0,0|purple-6,38.867782,-77.08811899999999,0,0|&center=38.870666,-77.0887715&zoom=10&rand=-1646228270&session=55e60cd9-00b6-001a-02b7-20ac-00163e7dd551",
            "startPoint": {
              "lng": -77.089424,
              "lat": 38.87355
            },
            "turnType": 6
          },
          {
            "signs": [],
            "index": 5,
            "maneuverNotes": [],
            "direction": 4,
            "narrative": "Stay straight to go onto S Walter Reed Dr.",
            "iconUrl": "http://content.mapquest.com/mqsite/turnsigns/rs_straight_sm.gif",
            "distance": 0.884,
            "time": 149,
            "linkIds": [],
            "streets": [
              "S Walter Reed Dr"
            ],
            "attributes": 0,
            "transportMode": "AUTO",
            "formattedTime": "00:02:29",
            "directionName": "South",
            "mapUrl": "http://www.mapquestapi.com/staticmap/v4/getmap?key=KEY&type=map&size=225,160&pois=purple-6,38.867782,-77.08811899999999,0,0|purple-7,38.855185999999996,-77.088584,0,0|&center=38.861484,-77.08835149999999&zoom=9&rand=-1646228270&session=55e60cd9-00b6-001a-02b7-20ac-00163e7dd551",
            "startPoint": {
              "lng": -77.088119,
              "lat": 38.867782
            },
            "turnType": 0
          },
          {
            "signs": [
              {
                "text": "120",
                "extraText": "",
                "direction": 0,
                "type": 545,
                "url": "http://icons.mqcdn.com/icons/rs545.png?n=120"
              }
            ],
            "index": 6,
            "maneuverNotes": [],
            "direction": 5,
            "narrative": "Turn slight left onto S Glebe Rd/VA-120.",
            "iconUrl": "http://content.mapquest.com/mqsite/turnsigns/rs_slight_left_sm.gif",
            "distance": 0.593,
            "time": 84,
            "linkIds": [],
            "streets": [
              "S Glebe Rd",
              "VA-120"
            ],
            "attributes": 0,
            "transportMode": "AUTO",
            "formattedTime": "00:01:24",
            "directionName": "Southeast",
            "mapUrl": "http://www.mapquestapi.com/staticmap/v4/getmap?key=KEY&type=map&size=225,160&pois=purple-7,38.855185999999996,-77.088584,0,0|purple-8,38.848926,-77.08122999999999,0,0|&center=38.852056,-77.08490699999999&zoom=10&rand=-1646228270&session=55e60cd9-00b6-001a-02b7-20ac-00163e7dd551",
            "startPoint": {
              "lng": -77.088584,
              "lat": 38.855186
            },
            "turnType": 7
          },
          {
            "signs": [],
            "index": 7,
            "maneuverNotes": [],
            "direction": 0,
            "narrative": "2400 S GLEBE RD is on the right.",
            "iconUrl": "http://content.mapquest.com/mqsite/turnsigns/icon-dirs-end_sm.gif",
            "distance": 0,
            "time": 0,
            "linkIds": [],
            "streets": [],
            "attributes": 0,
            "transportMode": "AUTO",
            "formattedTime": "00:00:00",
            "directionName": "",
            "startPoint": {
              "lng": -77.08123,
              "lat": 38.848926
            },
            "turnType": -1
          }
        ]
}

I'm using the json-c library. I tried using a loop and getting the index number within the array but I wasn't able to do it properly because I got errors.

How would I go about accessing the narratives section within the maneuvers?

Edit: The error I got was when I used json_object_array_length(maneuvers), it said that maneuvers wasn't a valid json object. Note that mauevers is already an array I parsed from a parent json-object

1 Answer 1

1

It would be helpful if you showed us some code, as well as what errors you're getting.

You should use json_object_from_file to read the JSON file and then json_object_object_get to get elements under a certain key.

Really bare bones code (error checks omitted, as well as type-checks - for example, I never check if maneuvers is indeed an array):

json_object *root = json_object_from_file("path/to/file.json");
json_object *maneuvers = json_object_object_get(root, "maneuvers");
int maneuvers_count = json_object_array_length(maneuvers);
for (int i = 0; i < maneuvers_count; i++)
{
    json_object *element = json_object_array_get_idx(maneuvers, i);
    json_object *narratives = json_object_object_get(element, "narratives");
    char *narratives_str = json_object_get_string(narratives);
}

See https://github.com/rbtylee/tutorial-jsonc/blob/master/tutorial/index.md for some tutorials.

Sign up to request clarification or add additional context in comments.

7 Comments

I'm writing the JSON to a string so I'm reading it from there. I used the method I found in this post. I kept getting the error in console that the array wasn't a valid JSON object
You should be able to use the the same json-c APIs for parsing the JSON. The JSON in your post looks valid (for example cat test.json | jq '.maneuvers[0].narrative' outputs "Start out going east on Clarendon Blvd toward N Queen St."). Again, some code you wrote for this problem would be helpful.
I wrote the exact same code you commented and got the following error (in the console, along with a debug error): "Assertion failed: json_object_get_type(jso) == json_type_array, file C:\vcpkg\builtrees\json-c\src\51ecf1a4a4-799e3998b0.clean\json_object.c, line 1231"
Please post a minimal example which reproduces the issue, see stackoverflow.com/help/minimal-reproducible-example
It's unclear from the json-c docs whether or not one should call json_object_put() on the array element object after obtaining it with json_object_array_get_idx(). For other functions the docs explicitly state that reference count is not changed and hence the put should not be called, but not for this function. So I assumed I must call the put. Can you tell if my understanding is correct?
This kind of question is better suited for their issue tracker. I really can't remember, I haven't used this library for a few years now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.