Updated with actual JSON Response, Messed up last time.
It is my second day with JSON, and i am stuck at the first step of my project.
i created a wcf rest service which gives this test json response.
[{
"busyEndTime":"\/Date(928164000000-0400)\/",
"busyStartTime":"\/Date(928164000000-0400)\/",
"endGradient":1.26743233E+15,
"startGradient":1.26743233E+15,
"status":"String content"
}]
i am trying to read the content of this output and use the content for various other purposes.
By content i am referring to the “busyEndTime, busyStartTime” values etc.
I have tried numerous examples on the net, but my bad luck continues,
following are the ways i tried to read the above, but failed.
$('#btnGetTime').click(function () {
$.ajax({
cache: false,
type: "GET",
async: false,
url: serviceAddress,
dataType: "application/json; charset=utf-8",
data: "{}",
success: function (student) {
******************* Try 1
var obj = jQuery.parseJSON(student);
for (var i = 0; i < obj.length; i++) {
alert(obj[i]);
}
********** Try 2
var obj = eval("(" + student + ")");
for (var i = 0; i < obj.length; i++) {
alert(obj[i]);
}
**************Try 3
success: test(student)
.......
.....
function test(jObject) {
var jArrayObject = jObject
if (jObject.constructor != Array) {
jArrayObject = new Array();
jArrayObject[0] = jObject;
}
**************Try 4
success: test(student)
.......
.....
function test(jObject) {
var jArrayObject = jObject
for (var i = 1, n = jObject.length; i < n; ++i) {
var element = jObject[i];
................
....................
}
*************** Try5
$.each(jArrayObject, function (key, value) {
alert(key + ": " + value);
});
I would really appreciate if some one could guide step by step, of how to read the JSON response like i have above and iterate over the array that the response contains and finally use the content that lies in the array, at least alert the key value pairs.
A quick response is all i want, i am loosing interest in jquery with each passing minute. 🙁
Update: Now that you’ve posted the actual JSON text, here’s an example of using it:
Live copy
Output:
Note that the dates aren’t automagically handled unless you use a “reviver” with the JSON parser that understands that particular date format. JSON has no date format of its own, but it has this concept of a “reviver” that can be used during the deserialization process to pre-process values.
jQuery’s own JSON parser doesn’t have the “reviver” feature, but you can download ones that do (there are three on Douglas Crockford’s github page — Crockford being the inventor of JSON). Then you’d tell jQuery not to parse the JSON, and instead do it explicitly yourself. That would look like this:
Live copy
…except of course you’d use some other JSON parser rather than
$.parseJSON.Original answer:
The problem
That’s not JSON. You can read up on JSON here, and you can validate your JSON strings here. I’m not quite sure what it is. It looks a lot like XML, but like someone took the XML from a tree viewer or something (those
-symbols next to the beginnings of elements).Below, I’ll show what that data might look like in JSON, how you would work with it, and then if you want to work with XML instead, the same example using XML data.
Your data in JSON format
Here’s an idea of what that might look like in JSON:
Note that the types (element names) have gone, because JSON has no concept of element names. (If you want them, you can create a key that holds the relevant information.) So each of the two entries in the array is a
busyDateTimeby virtue purely of being in theArrayOfBusyDateTime. But one of the things about JSON is that it’s very malleable, so you may prefer to do it slightly differently.Working with that JSON data
Here’s an example of using that data:
Live copy
Output:
XML
For completeness, if your data really is XML, like this:
Working with XML data:
…then here’s how you might work with that:
Live copy
…although I don’t work with XML much, could be some efficiencies to be had (seems like a lot of wrapping things up in jQuery instances).
Output: