Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
154
rated 0 times [  161] [ 7]  / answers: 1 / hits: 92263  / 11 Years ago, sat, september 21, 2013, 12:00:00

How can I alert the below JSON code using jquery?



{
results: {
course: CC167,
books: {
book: [
{
-id: 585457,
-title: Beginning XNA 20 game programming : from novice to professional,
-isbn: 1590599241,
-borrowedcount: 16
},
{
-id: 325421,
-title: Red Hat Linux 6,
-isbn: 0201354373,
-borrowedcount: 17
}
]
}
}
}


This is my json file content which can named result.json. I need to alert or print all data of this file using JavaScript or jQuery. How can I do this ?


More From » json

 Answers
8

Lets assume you have the JSON in String, var json_str = '{ results: ... }';



From there you have to parse it as JSON, this can be done using:



var json_obj = JSON.parse(json_str);


If instead you need to load from a file, use:



var json_obj;
$.getJSON(result.json, function (data) {
json_obj = data;
});


Once you have the JSON object, it is simple to access the data.



alert(json_obj.results.books.book[1][-title]); >>> Red Hat Linux 6


Or print the JSON as a whole:



alert(JSON.stringify(json_obj));

[#75543] Friday, September 20, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alejandro

Total Points: 231
Total Questions: 102
Total Answers: 107

Location: Jordan
Member since Wed, Jun 17, 2020
4 Years ago
alejandro questions
Mon, Jul 18, 22, 00:00, 2 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
Thu, Sep 10, 20, 00:00, 4 Years ago
;