Reputation: 131
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
Reputation: 148180
Try this
jonObj.files[0].Name
If you have in string then you need to use $.parseJSON
to convert it to json object.
var jonObj = $.parseJSON('{"files": [{"Thumbnail_url": null, "Name": "Chrysanthemum.jpg", "Length": 879394,"Type": "image/jpeg"}]}');
alert(jonObj.files[0].Name);
Upvotes: 2
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
Reputation: 57342
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
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
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
Reputation: 19173
Assuming your JSON response is stored in resp
.
alert(JSON.parse(resp).files[0].Name);
Upvotes: 3