0

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.........

Very basic JSON question

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.

1
  • You can read the file, line by line, and add them to an array. This way will give you the 'people array' and you will able to loop through it effortless. Commented May 23, 2012 at 11:09

4 Answers 4

1

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)
});
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0
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.

5 Comments

an object literal is not JSON.
then you can use eval function.
oh please god no - never - ever - use eval to parse JSON - use the built-in parser or a decent quality shim.
you can use any parser to parse json, this is just example.
in any event you haven't explained why the OPs "json" was wrong.
0

As already mentioned your JSON is not valid, if in doubt you can always check:

http://jsonlint.com/ or http://json.parser.online.fr/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.