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!!!
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!!!
Share Improve this question edited Jul 27, 2011 at 10:55 Curtis 103k69 gold badges277 silver badges359 bronze badges asked Jul 27, 2011 at 10:30 SPGSPG 6,20514 gold badges51 silver badges81 bronze badges3 Answers
Reset to default 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 ment, "instead of a single object [...] [you] got back an array of objects that [...] [you] have to access."
[]
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]
....
Just use an iterator
data[0].Symbol
etc.