0

I've got a problem with those data,

my json array data contains:

{"list_file":["{\"id\":\"511\",\"name\":\"Jellyfish.jpg\",\"projectId\":\"12539\",\"projectName\":\"project namessszd ddddzzde\",\"time\":\"1331843704\",\"size\":775702,\"timeRightFormat\":\"03\\\/15\\\/12 01:35:04 PM\",\"userFirstName\":\"Jerome\",\"userLastName\":\"Test\",\"userId\":\"8\"}","{\"id\":\"510\",\"name\":\"Hydrangeas.jpg\",\"projectId\":\"12539\",\"projectName\":\"project namessszd ddddzzde\",\"time\":\"1331843704\",\"size\":595284,\"timeRightFormat\":\"03\\\/15\\\/12 01:35:04 PM\",\"userFirstName\":\"Jerome\",\"userLastName\":\"Test\",\"userId\":\"8\"}","{\"id\":\"509\",\"name\":\"dudnzoizu ufoiuzio fueoifezuoiufifzeouofufzeoiuiofuz oife iofez.jpg\",\"projectId\":\"12539\",\"projectName\":\"project namessszd ddddzzde\",\"time\":\"1331843704\",\"size\":885242,\"timeRightFormat\":\"03\\\/15\\\/12 01:35:04 PM\",\"userFirstName\":\"Jerome\",\"userLastName\":\"Test\",\"userId\":\"8\"}"]}

when i loop through the elements like this

    $.each(data.list_file, function(i, file) {
        alert(file.id);
    });

I got undefined in the alert() box, but if i do just this instead

      $.each(data.list_file, function(i, file) {
        alert(file);
    });

I got the right json line.

Thank you

EDIT: with the entire array this time

6
  • because its simply treating it as a string and not as json? alert(typeof file); ? Commented Mar 15, 2012 at 20:45
  • Is that wrapped in a [], because what you have posted is not a valid JSON unless it is wrapped inside a []. If wrapped, then your function should work fine... jsfiddle.net/skram/F9Mrd Commented Mar 15, 2012 at 20:50
  • ok i've edited the array with the full json code this time Commented Mar 15, 2012 at 20:55
  • The json you just updated with also doesn't look valid. Can you give us the actual output you get from the json file when you visit it in a browser? Commented Mar 15, 2012 at 20:55
  • this is the one that i get from the browser and it's valid, i just tested it with jsonlint.com Commented Mar 15, 2012 at 20:57

4 Answers 4

2

Enclose the JSON in [], it'll work then. What you have is a json list.

And check out this Tinker.io

If you parse the JSON you can then use the normal notation for accessing the objects properties.

Validate the json here!

Good luck!

Sign up to request clarification or add additional context in comments.

1 Comment

my json is valid, actually i posted the entire array in edit ;)
1

I'm assuming you are making an ajax call. You need to specify the "json" datatype so that jquery will parse it as json.

$.ajax({
    url: url,
    dataType: "json",
    success: function (data) {
        // ...
    }
})

UPDATE:

Your json array is an array of json strings, try changing that to an array of objects:

{ "list_file": [ "{...}" ]}

should be

{ "list_file": [ {...} ]}

1 Comment

i'm pretty sure that's something like this ;)
1

What you have inside list_file is a string and not an Object. Actually, you have 3 strings.. each an object... Try using a function like below,

$.each(data['list_file'], function(i, file) {
    alert(JSON.parse(file).id);
});

Comments

0

this is not a valid json

you should wrap it with brackets !

you have many objects which must be in an Array.

Fix this first. later check.

3 Comments

data is in brackets this is only a part of my json array
Wait, it is an array, but should be in []
@Jerome so upload all the Json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.