Parsing the JSON data using jquery?
i'm having a problem with parsing the json data from hello.json to the current html file.
how do i get the data of investors, events and price from hello.json to three individual arrays.
http://compliantbox.com/mobile/optionsedge/hi.html
please help me out!!!
here is my html code:
<html>
<head>
<script src="http://code.jquery.com/jquery-1.4.4.js"></script>
</head>
<body>
<script>
$.getJSON("http://compliantbox.com/mobile/optionsedge/hello.json?jsoncallback=?",
{},
function(data) { alert(data); $.each(data.items, function(i,item){ alert(); });
});
</script>
</body>
</html>
and here is my json code:
开发者_StackOverflow{ "data":
{
"current_condition":[{
"investors": [{"active"},{"agressive"},{"conservative"},{"day trader"},{"very active"}],
"events": [{"3 months"},{"weekly"},{"monthly"},{"leaps"},{"heaps"}],
"price": [{"4"},{"3"},{"9"},{"5"},{"2"}]
} ] }}
Update 3: I just actually looked at your linked page (always copy relevant code into the question itself), and here's your problem:
$.each(data.items, function(i,item){
$("<html/>").attr("src", item.event);
if ( i == 3 ) return true;
});
There is no data.items
in the resulting object deserializing the (now-valid) JSON you're pointing to. There's no items
at all. Your JSON describes an object with one property, data
, which in turn has a property current_conditions
, which is an array with one entry, an object with the properties investors
, events
, and price
. See my code from Update 2 for how to interact with it.
Update 2: If you change your JSON to make it valid, jQuery will work. I mean, thousands of sites use this everyday... Live example with your data massaged to make it valid.
Update: Now that you've posted your JSON, we can see that it's invalid. Check out the JSON site and the JSONlint tool. Your investors
, events
, and price
arrays all contain entries in the form {"active"}
, which is an invalid object entry.
Original Answer:
If you're using jQuery's ajax
function (or getJSON
or one of its other wrappers), it should be parsed by the time you see it. If you ever have to parse a JSON string yourself, use parseJSON
. Details:
ajax
and its wrappers
Expanding on the ajax
thing: If you're getting back a string and expecting an object (e.g., the result of parsing the JSON), ensure that the server is returning the correct content-type ("application/json"). If you can't control the server and it's sending back the wrong content-type, you can override the server by giving ajax
a dataType
option.
If it's not working, you might want to check -- is the JSON really valid JSON? Because there's a lot of not-quite-JSON out there. For instance, this is valid JSON:
{
"foo": 1,
"bar": "x"
}
This is not:
{
foo: 1,
bar: 'x'
}
A strict parser may reject the latter, because it's invalid (in a couple of different ways).
Example - normal, server-is-configured-correctly version:
$.ajax({
url: "yoururl",
success: function(data) {
// Here, `data` is already an object if the response was
// JSON and the server gave the correct content-type
}
});
Example - Forcing the data type:
$.ajax({
url: "yoururl",
dataType: "json", // <== The new bit
success: function(data) {
// Here, `data` is already an object if the response was
// parseable JSON
}
});
or
$.getJSON("yoururl", function(data) {
// Here, `data` is already an object if the response was
// parseable JSON
});
Parsing your own string:
var str = '{"foo": 1, "bar": "x"}'; // A JSON string
var obj = jQuery.parseJSON(str); // The resulting object
Your JSON is invalid.
http://compliantbox.com/mobile/optionsedge/hello.json?jsoncallback=?
http://www.jsonlint.com/
That is why the parsing is not working.
As @Skilldrick pointed out in the Question comments, you should check the console when you're doing AJAX and it's not working; doing so would have revealed the syntax error exception.
Based upon the page you liked to and the JSON file it then calls, I would say that this would get you what you need:
function(data) {
var lists = data['data']['current_condition'][0];
var investors = lists['investors'],
events = lists['events'],
price = lists['price'];
}
精彩评论