2016-04-15 47 views
0

我在我的django應用程序中進行api調用。答案是json。目前我正在循環瀏覽模板中的數據並顯示錶格中的所有數據。在Django中處理Json響應

image_url以外的所有數據均顯示在表格中。 image_url有一個JPG路徑。這種類型的json需要與正常的string不同嗎?

我注意到有幾個字段缺少數據,我無法弄清楚爲什麼這幾個字段缺少數據和字段之前,他們都工作正常。

這是JSON對象之一

[ 
    { 
    "name": "_____", 
    "id": "_____", 
    "url": "https://api.deckbrew.com/mtg/cards/_____", 
    "store_url": "http://store.tcgplayer.com/magic/unhinged/_____?partner=DECKBREW", 
    "types": [ 
     "creature" 
    ], 
    "subtypes": [ 
     "shapeshifter" 
    ], 
    "colors": [ 
     "blue" 
    ], 
    "cmc": 2, 
    "cost": "{1}{U}", 
    "text": "{1}: This card's name becomes the name of your choice. Play this ability anywhere, anytime.", 
    "power": "1", 
    "toughness": "1", 
    "formats": {}, 
    "editions": [ 
     { 
     "set": "Unhinged", 
     "set_id": "UNH", 
     "rarity": "uncommon", 
     "artist": "Ron Spears", 
     "multiverse_id": 74252, 
     "flavor": "{1}: This card's flavor text becomes the flavor text of your choice. (This ability doesn't work because it's flavor text, not rules text (but neither does this reminder text, so you figure it out).)", 
     "number": "23", 
     "layout": "normal", 
     "price": { 
      "low": 0, 
      "median": 0, 
      "high": 0 
     }, 
     "url": "https://api.deckbrew.com/mtg/cards?multiverseid=74252", 
     "image_url": "https://image.deckbrew.com/mtg/multiverseid/74252.jpg", 
     "set_url": "https://api.deckbrew.com/mtg/sets/UNH", 
     "store_url": "http://store.tcgplayer.com/magic/unhinged/_____?partner=DECKBREW" 
     } 
    ] 
    }, 

這是我如何在視圖中

def graphs(request): 
    if request.user.is_authenticated(): 
     data = [] 
     r = requests.get('https://api.deckbrew.com/mtg/cards') 
     jsonList = r.json() 
     for cards in jsonList: 
      data.append(cards) 
     return render(request, 'graphs/graphs.html', {'data': data}) 
    else: 
     return redirect('index') 

這是我如何在模板 標誌着我訪問數據處理數據那些失蹤的人。

  {% for card in data %} 
      <tr> 
      <td>{{ card.name }}</td> 
      <td>{{ card.id }}</td> 
      <td>{{ card.url }}</td> 
      <td>{{ card.store_url }}</td> 
      <td>{{ card.types }}</td> 
      <td>{{ card.subtypes }}</td> 
      <td>{{ card.colors }}</td> 
      <td>{{ card.cmc }}</td> 
      <td>{{ card.cost }}</td> 
      <td>{{ card.text }}</td> 
      <td>{{ card.power }}</td> 
      <td>{{ card.toughness }}</td> 
      <td>{{ card.formats }}</td> 
      <td>{{ card.editions }}</td> 
      <td>{{ card.set }}</td>  # MISSING 
      <td>{{ card.set_id }}</td> # MISSING 
      <td>{{ card.rarity }}</td> # MISSING 
      <td>{{ card.artist }}</td> # MISSING 
      <td>{{ card.multiverse_id 
      <td>{{ card.flavor }}</td> # MISSING 
      <td>{{ card.number }}</td>  # MISSING 
      <td>{{ card.layout }}</td>  # MISSING 
      <td>{{ card.price }}</td>  # MISSING 
      <td>{{ card.low }}</td>   # MISSING 
      <td>{{ card.median }}</td>  # MISSING 
      <td>{{ card.high }}</td>  # MISSING 
      <td>{{ card.url }}</td> 
      <td>{{ card.image_url }}</td> # MISSING 
      <td>{{ card.set_url }}</td>  # MISSING 
      <td>{{ card.store_url }}</td> 
      </tr> 
      {% endfor %} 

enter image description here

+0

對不起,我失去了你。你說響應是json,但你的視圖函數中的響應顯然是html? – Wtower

+0

第一個代碼塊是json。然後在我看來,我把json放在了一個列表中。然後我在html模板中訪問它 – wuno

+0

我複製並粘貼了json中的第一個對象。這是我的代碼。你說這裏的問題是我得到的JSON響應是不是有效的JSON? – wuno

回答

1

他們不是失蹤,他們是不是card一部分,他們的editions對象的一部分,所以你需要遍歷該列表。

{% for card in data %} 
    {% for edition in card.editions %} 
     <td> {{edition.set }}</td> 
    {% endfor %} 
{% endfor %} 
+0

那麼爲什麼multiverse_id仍然顯示? – wuno

+0

@wuno - 如果它自從缺少結束標記,我會感到驚訝 – Sayse

+0

這是正確的我的應用程序是我的問題中的拼寫錯誤。是的,它確實顯示了我的信息。這就是我之前和之後所缺少的數據。我可以看到爲什麼有些顯示和有些顯示不需要將數據添加到數據中?我是否應該將版本附加到數據並以相同的方式訪問它?數據卡中的data.append(editons)卡片.image_url – wuno