0

i have a php function which returns me this code in JSON

{"0":{"title":"Dans l\u2019appartement"},"1":{"title":"A l\u2019a\u00e9roport - D\u00e9part de B\u00e9atrice"},"2":{"title":"Visite chez Jean-Louis"},"3":{"title":"Anita \u00e0 la matenit\u00e9"},"4":{"title":"Visite chez Jean-Louis 2"},"5":{"title":"H\u00e9l\u00e9na pr\u00e9sent\u00e9e \u00e0 la famille"},"6":{"title":"Chez des proches"},"7":{"title":"Soir\u00e9e souvenir avec un proche - Photos, histoires"},"8":{"title":"Douceline tenant un b\u00e9b\u00e9"},"9":{"title":"Visite chez Jean-Louis 3"},"10":{"title":"Bapt\u00eame de Alexandra - Dans l\u2019\u00e9glise"}}

and i want to manipulated in JQuery I’ve did this but it doesn’t work

$.each(json, function(item, value) {
            console.log(value);
                $.each(value, function() {
                   console.log(this.title);
                });
            });

any ideas thanks a lot

2
  • assuming that blob has the variable name "json" couldn't you just do this ` for(var key in json){ console.log(key + ' title: ' + json[key].title); };` Commented Jul 19, 2014 at 18:31
  • if you sent data as array of objects it simplifies things a bit [{"title":"..."},{"title":"..."}] Commented Jul 19, 2014 at 18:36

1 Answer 1

1

The first traversal using each provides you the object containing the value of title field.

So , Simply fetch the value using :

$.each(json, function(item, value) {
     alert(value.title);        
 });

And here is the demo jsfiddle

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

4 Comments

i've done wath you have told me it's show me this error ` Uncaught TypeError: Cannot use 'in' operator to search for '986' in #<error>`
You get the value of json from AJAx call ... if yes make sure that you use dataType : 'json' .... or you could parse the JSON data first using var json = $.parseJSON(data);
it's working now after i've added the parse line, but in my php function that i call in ajax i've write echo json_encode($test) it's not enough ?
That is a must in server side (to send JSON) ... but the client side script need to know that which format/type of data is being received. And hence the necessity of parsing. .... (And if you are using AJAX i recommend you to use dataType : 'json' instead)

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.