Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

Using the json-c library to parse a json array

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

question from:https://stackoverflow.com/questions/65914522/using-the-json-c-library-to-parse-a-json-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...