Reputation: 113
Hi I'm a complete newbie to json. I'm trying to parse a json file into a web page just like explained in this thread.........
However my json file is a large list of contacts that are not in an array like 'people' in the above example. instead the first 6 lines of my json file are like the following.
{ "first_name": "Tom", "last_name": "Moore", "phone": "123456", "email": "[email protected]" }
{ "first_name": "Fred", "last_name": "Power", "phone": "197412", "email": "[email protected]" }
{ "first_name": "Ann", "last_name": "Doyle", "phone": "836547", "email": "[email protected]" }
{ "first_name": "Phil", "last_name": "Jones", "phone": "927481", "email": "[email protected]" }
{ "first_name": "Jane", "last_name": "Ross", "phone": "993377", "email": "[email protected]" }
{ "first_name": "Tom", "last_name": "Moore", "phone": "123456", "email": "[email protected]" }
How can I loop through them to display like in the above thread? Many thanks in advance.
Upvotes: 0
Views: 536
Reputation: 170
As already mentioned your JSON is not valid, if in doubt you can always check:
http://jsonlint.com/ or http://json.parser.online.fr/
Upvotes: 0
Reputation: 2615
var t = eval('[{ "first_name": "Tom", "last_name": "Moore", "phone": "123456", "email": "[email protected]" },{ "first_name": "Fred", "last_name": "Power", "phone": "197412", "email": "[email protected]" },{ "first_name": "Ann", "last_name": "Doyle", "phone": "836547", "email": "[email protected]" },{ "first_name": "Phil", "last_name": "Jones", "phone": "927481", "email": "[email protected]" },{ "first_name": "Jane", "last_name": "Ross", "phone": "993377", "email": "[email protected]" },{ "first_name": "Tom", "last_name": "Moore", "phone": "123456", "email": "[email protected]" }]');
for (i in t) {
var data = t[i];
for (k in data) {
alert(k+" >> "+data[k])
}
}
you can get "k" as key and "data[k]" as value.
Upvotes: 0
Reputation: 19237
this is not json format then, so you won't be able to handle it with functions that are for json. I would suggest to use a lib/function on the server side to create correct json output, then it'll be much easier to use it in the client side as well.
Upvotes: 0
Reputation: 97591
Your json is invalid. It needs square brackets around the outside, and commas between items. It should be:
[
{ "first_name": "Tom", "last_name": "Moore", "phone": "123456", "email": "[email protected]" },
{ "first_name": "Fred", "last_name": "Power", "phone": "197412", "email": "[email protected]" },
{ "first_name": "Ann", "last_name": "Doyle", "phone": "836547", "email": "[email protected]" },
{ "first_name": "Phil", "last_name": "Jones", "phone": "927481", "email": "[email protected]" },
{ "first_name": "Jane", "last_name": "Ross", "phone": "993377", "email": "[email protected]" },
{ "first_name": "Tom", "last_name": "Moore", "phone": "123456", "email": "[email protected]" }
]
$.getJSON
handles the json parsing for you - just do:
$.getJSON('/url/of/the/json/file', function(people) {
alert(people[0].first_name)
});
Upvotes: 1