Reputation: 6555
I want to get each name
and logo
out of my returned data, but I cannot. The issue could be that the value of JSON comes out of the database and is escaped too.
How can I get each name
and logo
from the data I posted below?
Code:
$.getJSON('/sms/fetch_smartpages/', function (data) {
var items = [];
$.each(data, function (key, val) {
$('#linkHolder').append('<p>' + val + '</p>');
items.push('<li id="' + key + '">' + val.fields.JASON.NAME + '</li>');
});
});
This is the data as it comes back:
[
{
"pk": 11,
"model": "content",
"fields": {
"logo": "",
"json": "{\"name\":\"sas\",\"logo\":\"\",\"theme\":\"a\",\"pages\":[{\"id\":\"1366993986180\",\"name\":\"Page Name\",\"type\":\"basic\",\"components\":{\"img\":\"\",\"text\":\"\"}}]}",
"link": null,
"name": "sas",
"user": 2
}
},
{
"pk": 18,
"model": "content",
"fields": {
"logo": "",
"json": "{\"name\":\"test123456789\",\"logo\":\"\",\"theme\":\"e\",\"pages\":[{\"id\":\"1366994946585\",\"name\":\"Page Name\",\"type\":\"basic\",\"components\":{\"img\":\"\",\"text\":\"\"}}]}",
"link": 18,
"name": "test123456789",
"user": 2
}
}
]
Upvotes: 0
Views: 629
Reputation: 13821
It seems that the json
element of val.fields
actually is a string, not a json object. So val.fields.json.name
will not work, as val.fields.json
is not a json object. You either have to fix the server so it returns a proper json object at this location, or you need to parse that particular fields to JSON.
Edit
However, it appears that name
and logo
also are set directly to var.fields
, so var.fields.name
and var.fields.logo
should work.
Upvotes: 1
Reputation: 812
Since you are using jQuery, try using the parseJSON function to create a dictionary. Then you can get name and logo like a normal field.
http://api.jquery.com/jQuery.parseJSON/
Upvotes: 1
Reputation: 40318
$.each(data, function (index, val) {
items.push('<li id="' + index+ '">' + val.fields.name + '</li>');
});
Upvotes: 1
Reputation: 15387
Try this but not tested
items.push('<li id="' + key + '">' + val.fields.name + '</li>');
Upvotes: 1