vidyasagar85
vidyasagar85

Reputation: 131

How to read json response jquery

I am getting this string as json response from my handler. And now i want to display only the Name value in alert, just to test the functionality.... How can i do this?

String is in below:

{
    "files": [
        {
            "Thumbnail_url": null,
            "Name": "Chrysanthemum.jpg",
            "Length": 879394,
            "Type": "image/jpeg"
        }
    ]
}

Upvotes: 0

Views: 481

Answers (6)

Adil
Adil

Reputation: 148180

Try this

Live Demo

jonObj.files[0].Name

If you have in string then you need to use $.parseJSON to convert it to json object.

Live Demo

var jonObj = $.parseJSON('{"files": [{"Thumbnail_url": null, "Name": "Chrysanthemum.jpg", "Length": 879394,"Type": "image/jpeg"}]}');
alert(jonObj.files[0].Name);​

Upvotes: 2

Jai
Jai

Reputation: 74738

If you are using jQuery then in the success function pass the data as param and access it in the .each() loop.

This should be this way:

$.each(data.files, function(i, el) {
  alert(el.name);
});

Upvotes: 0

NullPoiиteя
NullPoiиteя

Reputation: 57332

for that you can use jQuery.parseJSON it does Takes a well-formed JSON string and returns the resulting JavaScript object.

var obj = jQuery.parseJSON('{"files":[{"Thumbnail_url":null,"Name":"Chrysanthemum.jpg","Length":879394,"Type":"image/jpeg"}]}');

and than

 obj.files[0].Name

Upvotes: 0

palaѕн
palaѕн

Reputation: 73966

//Note the jQuery.parseJSON function
var response = jQuery.parseJSON(JSON_Response);
$.each(response, function(object) {
    $.each(response[object], function(values) {
        console.log(response[object][values].Name)
        console.log(response[object][values].Length)
    });
})​

Upvotes: 0

sachleen
sachleen

Reputation: 31141

First you need to parse the JSON:

var result = JSON.parse('{"files":[{"Thumbnail_url":null,"Name":"Chrysanthemum.jpg","Length":879394,"Type":"image/jpeg"}]}')

Then you can refrence it using:

result.files[0].Name

Upvotes: 1

tom
tom

Reputation: 19163

Assuming your JSON response is stored in resp.

alert(JSON.parse(resp).files[0].Name);

Upvotes: 3

Related Questions