【问题标题】:Using the json-c library to parse a json array使用json-c库解析一个json数组
【发布时间】:2021-01-27 07:21:23
【问题描述】:

我有以下 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-object 解析的数组

【问题讨论】:

    标签: arrays c json json-c


    【解决方案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

    【讨论】:

    • 我正在将 JSON 写入一个字符串,因此我正在从那里读取它。我使用了我在这个post 中找到的方法。我一直在控制台中收到数组不是有效的 JSON 对象的错误
    • 您应该能够使用相同的 json-c API 来解析 JSON。您帖子中的 JSON 看起来有效(例如 cat test.json | jq '.maneuvers[0].narrative' 输出 "Start out going east on Clarendon Blvd toward N Queen St.")。同样,您为这个问题编写的一些代码会有所帮助。
    • 我编写了与您评论完全相同的代码并收到以下错误(在控制台中,以及调试错误):“断言失败:json_object_get_type(jso) == json_type_array, file C:\vcpkg \builtrees\json-c\src\51ecf1a4a4-799e3998b0.clean\json_object.c,第 1231 行
    • 请发布一个重现问题的最小示例,请参阅stackoverflow.com/help/minimal-reproducible-example
    猜你喜欢
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-03
    相关资源
    最近更新 更多