0

我从服务器返回了以下 JSON 数据,但我无法弄清楚如何解析操作数组以进行叙述:

{
"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
          }
        ]
}

我正在使用json-c库。我尝试使用循环并获取数组中的索引号,但由于出现错误而无法正确执行。

我将如何访问演习中的叙述部分?

编辑:我得到的错误是当我使用 json_object_array_length(maneuvers) 时,它说机动不是有效的 json 对象。请注意,mauevers 已经是我从父 json 对象解析的数组

4

1 回答 1

1

如果您向我们展示一些代码以及您遇到的错误,那将会很有帮助。

您应该使用json_object_from_file读取 JSON 文件,然后json_object_object_get获取某个键下的元素。

真正的基本代码(省略了错误检查以及类型检查 - 例如,我从不检查是否maneuvers确实是一个数组):

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);
}

有关一些教程,请参阅https://github.com/rbtylee/tutorial-jsonc/blob/master/tutorial/index.md

于 2021-01-27T12:10:17.860 回答