2015-02-24 56 views
0

我有一個來自API的JSON響應。 JSON響應「結果」是一個包含多個單獨結果的列表,稱爲「路由」。將標籤值添加到JSON

對於每個結果,在結果JSON響應中,我希望添加更多信息,例如API中生成此響應的請求中包含的內容。

三個縮短結果(「路線」)的一個例子,整體結果中如下所示:

results = 
[({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'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'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'})] 

我想補充它們各自的值。對於3個結果上面的例子,有3個起源COORDS,如:

origincoords = ['51.41833327,0.115963078', '51.34666046,-0.210947524', '51.39574919,-0.045778021']  

UPDATE - 由於蒂諾,我已經能夠正確地插入的值。但是,我現在好奇我可以如何插入不同的位置。例如,爲了實現:

最終結果看會是什麼樣子 -

({u'routes': [{u'bounds': {u'northeast': {u'lat': value, 
               u'lng': value}, 
           u'southwest': {u'lat': value, 
               u'lng': value}, 
           u'origincoords': '51.39574919,-0.045778021'}, 
        u'copyrights': u'value', 
        u'overview_polyline': {u'points': u’value’}, 
        u'summary': u’value’, 
        u'warnings': [], 
        u'waypoint_order': []}], 
     u'status': u'OK'})] 
+1

這些「標籤」是通常被稱爲與要添加到「結果」列表中的每個字典中的值相關聯的鍵。 – martineau 2015-02-24 22:18:13

+0

這非常有用。我必須繼續建立我的詞彙。非常感謝不只是答案,但也爲輔導 – LearningSlowly 2015-02-25 08:39:13

回答

1

什麼你說你要沒有引起太大的意義:

({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'origincoords': '51.39574919,-0.045778021'})] 

但這:

({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', # NOT the end of the dict 
     u'origincoords': '51.39574919,-0.045778021'})] 

一個簡單的方法來獲得後者,它採用izip(或zip),將是:

try: 
    from itertools import izip 
except ImportError: 
    izip = zip # Python 3 

origincoords = ['51.41833327,0.115963078', 
       '51.34666046,-0.210947524', 
       '51.39574919,-0.045778021'] 

for route, origincoord in izip(results, origincoords): 
    route['origincoord'] = origincoord 

更新

如果你不是想在對插入到裏面的'bounds'詞典第一個,[0],在results列表中的每個字典的'routes'列表中的字典,可以通過使用以下來完成:

for route, origincoord in izip(results, origincoords): 
    route['routes'][0]['bounds']['origincoord'] = origincoord 

注意兩個列表和字典使用[x]符號來訪問他們的內容。 x必須求值爲列表的整數索引或字典的可哈希對象(例如,字符串,整數,元組等)。

+0

我感激不盡。謝謝。 – LearningSlowly 2015-02-25 08:38:05

+0

在我想將鍵和值插入到JSON響應中的其他位置的情況下,我將如何指定此索引? – LearningSlowly 2015-02-25 15:09:52

+0

這取決於您想要插入的響應的哪一部分。在最高級別,「結果」本身是一個字典列表,所以要在該列表中插入另一個項目,您可以使用'results.insert()'。要在「結果」中插入(或替換)其中一個最上面的字典中的條目,您可以使用「結果」[] [] = '。爲了更容易地看到響應中的數據結構,請嘗試'import json',然後'print(json.dumps(results,indent = 4))'。 – martineau 2015-02-25 16:44:17

0

zip函數返回的元組,其中第i元組包含來自每個第i個元素的列表參數序列。要添加座標的結果可能要遍歷元組的列表和分配給它的for循環:

for result, coord in zip(results, origincoords): 
    result[u'origincoords'] = coord 
1

這裏是做什麼的,沒有任何壓縮的一個簡單的方法

for index in range(len(results)): 
    results[index][u'origincoords'] = origincoords[index] 
+0

謝謝@Parind。如何更改[index]以將值插入到其他位置,例如['routes'] [0] ['legs'] [0] – LearningSlowly 2015-02-25 14:28:54

+0

results [0] [「routes」] [0] [ 「bounds」] [「new_element」] =「new_data」 – LuckyStarr 2015-02-25 19:50:59