1

I am trying to parse the following JSON using Python / Django:

[
  {
    "street": "KEELER",
    ":id": 1421
  }
]

Within my Django templates, I can successfully access the street key like:

{{ obj.street }}

but cannot access the id. I have tried the following (all taken from various SO questions):

{{ obj.id }} , {{ obj.:id }}, {{ obj[':id'] }}

I have seen the couple of other questions in SO addressing a similar issue, but none seem to help.

4

3 回答 3

2

Your object is wrapped in an array.

obj = [
  {
    "street": "KEELER",
    ":id": 1421
  }
]

:id should be accessed like obj[0][':id'].

于 2013-06-17T15:57:01.797 回答
1

So as @Aya recommended, what I did was dump the JSON to a string, replace all instances of ":id" with "id", then convert it back to JSON. At that point, I was able to access the ID like:

{{ obj.id }}
于 2013-06-17T18:24:21.180 回答
0

You could write your own parser and just parse the json as a string, there you can add custom parsing features to the id field. Although being in java, this might help

于 2013-06-17T15:55:22.540 回答