0

I know how to use jquery to parase simple json.

json

 {
        "Symbol": "AAL.L",
        "Name": "ANGLO AMERICAN",
        "Last": "3061.50",
        "Date": "7/26/2011",
        "Time": "11:35am",
        "Change": "+35.00(+1.16%)",
        "High": "3087.00",
        "Low": "3047.00",
        "Volume": "3160820",
        "Bid": "3061.50",
        "Ask": "3062.50",
        "PercentChange": "+1.16%"
    }

jquery

 $.getJSON("1.json", function(data) {
                    $("div").html("<p>Symbol="+data.Symbol+" Time="+data.Time+" Bid="+data.Bid+"</p>");
             });

But, now I have a json file like:

[
    {
        "Symbol": "AAL.L",
        "Name": "ANGLO AMERICAN",
        "Last": "3061.50",
        "Date": "7/26/2011",
        "Time": "11:35am",
        "Change": "+35.00(+1.16%)",
        "High": "3087.00",
        "Low": "3047.00",
        "Volume": "3160820",
        "Bid": "3061.50",
        "Ask": "3062.50",
        "PercentChange": "+1.16%"
    },
    {
        "Symbol": "AAL.L",
        "Name": "ANGLO AMERICAN",
        "Last": "3061.50",
        "Date": "7/26/2011",
        "Time": "11:35am",
        "Change": "+35.00(+1.16%)",
        "High": "3087.00",
        "Low": "3047.00",
        "Volume": "3160820",
        "Bid": "3061.50",
        "Ask": "3062.50",
        "PercentChange": "+1.16%"
    },
    {
        "Symbol": "AAL.L",
        "Name": "ANGLO AMERICAN",
        "Last": "3061.50",
        "Date": "7/26/2011",
        "Time": "11:35am",
        "Change": "+35.00(+1.16%)",
        "High": "3087.00",
        "Low": "3047.00",
        "Volume": "3160820",
        "Bid": "3061.50",
        "Ask": "3062.50",
        "PercentChange": "+1.16%"
    }
]

I can't use that way to parase. the result is "undefined". How can I parase this. Additionly, I want the result is like:

Symbol = [num, num, num], Time = [num, num, num], Bid = [num, num, num]

Could someone tell me how to do that. Many thanks!!!

3 Answers 3

4
$.getJSON("1.json", function(data) {
    var i = 0, dataSize = data.length, html = '';

    for(i; i < dataSize; i++){
        html += "<p>Symbol="+data[i].Symbol+" Time="+data[i].Time+" Bid="+data[i].Bid+"</p>";
    }

    $("div").html(html);
});

Edit

Based on @Robert Koritnik's comment, "instead of a single object [...] [you] got back an array of objects that [...] [you] have to access."

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

2 Comments

You should explain a little why this works, because OP is obviously not as seasoned with Javascript and JSON. Maybe they don't know that instead of a single object they got back an array of objects that they have to access.
@Robert Koritnik: It works, because JavaScript just works. There are merely five core lines of code to wrap the head around. The OP didn't ask why his version is not working, it was already obvious, a solution was sought to adjust for the new object. I will add your suggestion, just to make you happy.
1

[] means array so you need to access the items by key like data[0]. you can loop over them with for or $.each().

$.getJSON("1.json", function(data) {
  $.each(data, function(i,obj){
    $("div").html("<p>Symbol="+obj.Symbol+" Time="+obj.Time+" Bid="+obj.Bid+"</p>");
  });                    
});

If you want the result restructured then youd have to loop over it in the same fashion and create a new structure. ID give an example but i dont understand where you are going ith [num,num,num]....

5 Comments

It's so bad performance-wise to change the DOM for every iteration. Also, $.each is way slower than the native for loop.
@Shef: Slower may be... But decision which one to use should not entirely rely on speed alone.
@shef: I agree with you on the DOM manipulation on every iteration... but as far as $.each there more to it than performance. IF raw prformance was the only concern then no on would use jquery or another lib at all.
@Robert Koritnik: Yes, it is slower, and I don't see any advantage here on using the $.each. If you do, please, enlighten me!
@prodigitalson: Using jQuery is one thing, overusing it is totally another one. jQuery is the Swiss army knife of DOM manipulation, don't abuse it.
0

Just use an iterator

 data[0].Symbol

etc.

Comments

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.