[Answer]-Parsing JSON(query set) from django view

1👍

Presuming you’ve got a variable called response containing the content of the AJAX response, you want something like this:

loaded_data = json.loads(response)
for record in loaded_data:
    print record["fields"]["name"]

0👍

If the return value of django view is a list then

## Lets say variable data contains your query response
    data = [
    {"pk": 3,
     "model": "alongjs.carmodel",
     "fields":
          {"car": 2, "name": "city-unlimited"}},


    {"pk": 4,
     "model": "alongjs.carmodel",
     "fields":
         {"car": 2, "name": "hill-to-city"}}
]
for x in data:
    print x['fields']['name']

0👍

While this question has been brilliantly been answered here . I have found another way.

  var to_parse = jQuery.parseJSON(data);
  var temp = '';

  for (var i in to_parse)
       {
        temp += "<option>" + to_parse[i].fields.name + "</option>";
   }

 alert(temp)

I have Used jQuery.parseJSON.

👤rgm

Leave a comment