0

I can get the 'name' values but not the 'Orders' seems the Orders are returned as object not Array?

json.json

[{ "ID":"23",
"LastName":"Moe",
"FirstName":"Ronnie",
"Orders":[{"OrderNumber":"11","ItemNumber":"22"},
             {"OrderNumber":"33","ItemNumber":"44"}]}]

myHTML.html

$.getJSON('json.json',  
    {},
    function (data) {  
     $.each( data, function ( i, val ) {
        var lastname=this.LastName; 
        var firstname=this.FirstName;
        var orders=this.Orders;

        $.each(data.Orders, function(property, value) {
        alert(property + "=" + value);

        //Insert the data to HTML page
        $(".title").append('<li>'+lastname+', '+firstname+'  >'+'</li>');
        $(".title").append('<li>'+orders+'</li>');

        });

    });
    }) 
 });  
5
  • See this fiddle jsfiddle.net/KyleMuir/jcr79/3 as mentioned below - you are iterating over data twice. Commented Sep 20, 2013 at 1:33
  • still returns [object] and not value - I updates jsfiddle.net to show results with alert..? Commented Sep 20, 2013 at 1:40
  • Thats because alert will toString it, console.log will log the whole object. If you switched it to alert(value.OrderNumber) or something similar you will see values. Commented Sep 20, 2013 at 1:45
  • Thank you so much, that works! I was confused with the object - how do I give you credit for this answer? Commented Sep 20, 2013 at 1:59
  • I'll post it as an answer and you can accept it :) glad to be of assistance. Commented Sep 20, 2013 at 2:00

2 Answers 2

2

Should be this.Orders or val.Orders, not data.Orders

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

Comments

1

See this fiddle http://jsfiddle.net/KyleMuir/jcr79/3 as mentioned below - you are iterating over data twice. You need to point to the correct datasource.

As for the alert, you need to alert the properties for the object, not the whole object. E.g. alert(value.OrderNumber)

Alert will simply print out the string representation of the object ("[object object]").

Console.log will log the entire object.

Hope this helps :)

3 Comments

one last question: on jsfiddle the console.log didn't show any output? I'm new to this, where would I see the run results?
If you have chrome and hit f12 it will bring up the developer tools. From there you can open the console. Ctrl + shift + J is the shortcut to directly open the console (the icon starts with this symbol > followed by 3 horizontal lines). In the fiddle provided you should see 2 Objects that are expandable in the console. Expanding them should show you the attributes an object has and the values they contain.
Also, this is the definitive guide to the developer tools in Chrome developers.google.com/chrome-developer-tools/docs/console. IE and firefox both have these, I am just familiar with Chrome as it's my browser of choice.

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.