0

How do i parse this JSON string:

[
    {"pk": 3,
     "model": "alongjs.carmodel",
     "fields":
          {"car": 2, "name": "city-unlimited"}},


    {"pk": 4,
     "model": "alongjs.carmodel",
     "fields":
         {"car": 2, "name": "hill-to-city"}

    }
]

I only want to get fields['name']. this is being returned from Django view. I have serialised the query set to json. This is being retuned in ajax response.

1

3 Answers 3

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"]
Sign up to request clarification or add additional context in comments.

Comments

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']

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.