简体   繁体   English

根据JSON响应的状态码将数据追加到JSON响应

[英]Appending data to JSON response depending on status code of JSON response

I have been using the following to append information to a JSON response. 我一直在使用以下内容将信息附加到JSON响应中。

for result in results:
    if result["status"] == "OK":
        for route, origincoord in izip(results, origincoords):
            route['routes'][0]['legs'][0][u'_sent_origin'] = origincoord

Where origincoords is a list of coordinates. 其中origincoords是坐标列表。 The number of origincoords is equal to the number of JSON dictionaries I have. origincoords的数量等于我拥有的JSON字典的数量。

This works fine assuming the JSON response returned is valid (ie "Status": 'OK'). 假设返回的JSON响应有效(例如,“状态”:“确定”),此方法就可以正常工作。

However, if for whatever reason it is not (for example, Unknown_Error, Not_Found etc) the above will fail as the relevant origincoords cannot be paired with their relevant JSON response. 但是,如果出于某种原因(例如,Unknown_Error,Not_Found等)并非如此,则上述操作将失败,因为相关的origincoords无法与其相关的JSON响应配对。 1. the integrity of the data is lost and 2. there isnt an equal amount of JSON dictionaries to origincoords thus an index error is raised and 3.the location to insert the data is not possible with the path given ['routes'][0]['legs'][0][u'_sent_origin'] as it wont exist when a status != "OK" is returned. 1.数据的完整性丢失,并且2.原始字典的JSON字典数量不相等,从而导致索引错误,并且3.使用给定的路径['routes'],无法插入数据。 0] ['legs'] [0] [u'_sent_origin'],因为返回状态==“确定”时将不存在。

I have tried the following to overcome this - 我尝试了以下方法来克服这一点-

for result in results:
    for route, origincoord in izip(results, origincoords):
        if result["status"] == "OK":
            route['routes'][0]['legs'][0][u'_sent_origin'] = origincoord
        if result["status"] != "OK":
            route[u'_sent_origin'] = origincoord

However this also returns an IndexError 但是,这也会返回IndexError

    route['routes'][0]['legs'][0][u'_sent_origin'] = origincoord
IndexError: list index out of range

How do I maintain the integrity of the appended information to their JSON responses in the scenario of a JSON response with an error? 在出现错误的JSON响应的情况下,如何保持附加信息对其JSON响应的完整性?

EDIT. 编辑。 My desired outcome. 我想要的结果。

In this example, the first result has an unknown error, the second is OK and the third has an unknown error. 在此示例中,第一个结果具有未知错误,第二个结果正常,第三个结果具有未知错误。

origincoords = ['51.51964085,-0.092434321',
 '51.51963442,-0.092433965',
 '51.52208762,-0.095990014']

results = [{u'routes': [], u'status': u'UNKNOWN_ERROR'},
{u'routes': [{u'bounds': {u'northeast': {u'lat': value,
                                              u'lng': value},
                               u'southwest': {u'lat': value,
                                              u'lng': value}},
                   u'copyrights': u'value',
                   u'overview_polyline': {u'points': u’value’},
                   u'summary': u’value’,
                   u'warnings': [],
                   u'waypoint_order': []}],
      u'status': u'OK'},
    {u'routes': [], u'status': u'UNKNOWN_ERROR'}]

I would like to pair the first set of origincoords to the first JSON response, the second to the second JSON etc. 我想将第一组origincoords与第一个JSON响应配对,将第二组与第二个JSON等配对。

Thus 从而

 results = [{u'routes': [], u'status': u'UNKNOWN_ERROR', u'_sent_origin': '51.51964085,-0.092434321'},
    {u'routes': [{u'bounds': {u'northeast': {u'lat': value,
                                                  u'lng': value},
                                   u'southwest': {u'lat': value,
                                                  u'lng': value}},
                       u'copyrights': u'value',
                       u'overview_polyline': {u'points': u’value’},
                       u'summary': u’value’,
                       u'warnings': [],
                       u'waypoint_order': []}],
          u'status': u'OK',
          u'_sent_origin': '51.51963442,-0.092433965'}],
        {u'routes': [], u'status': u'UNKNOWN_ERROR', u'_sent_origin': '51.52208762,-0.095990014'}]

Your complete set-up is not clear to me from the snippet you posted, but it seems you need to check the status before iterating over the izip . 从您发布的代码片段中,您还看不到您的完整设置,但是似乎需要在迭代izip之前检查状态。

for result in results:
        if result["status"] == "OK":
            for route, origincoord in izip(results, origincoords):
                route['routes'][0]['legs'][0][u'_sent_origin'] = origincoord
        else
            for route, origincoord in izip(results, origincoords):
                route[u'_sent_origin'] = origincoord

I was doing two loops instead of one! 我正在做两个循环,而不是一个!

Solution - 解决方案-

for route, origincoord in izip(results, origincoords):
    if route["status"] == "OK":
        route['routes'][0]['legs'][0][u'_sent_origin'] = origincoord
    else:
        route[u'_sent_origin'] = origincoord

I didn't need to do two loops. 我不需要做两个循环。 I was doing 'for result in results' and 'for route, origincoord in izip(results, originscoords):' 我在做“为结果求结果”和“为路线,izip中的origincoord(结果,originscoords):”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM