user155814
user155814

Reputation: 101

Parsing JSON array in JQuery

I'm trying to get the hand on JQuery and JSON using an ASP.NET webservice. The Webservice returns this result:

{
    MyResult: {
        Ticket: {
            "Author": "rd",
            "CssClass": "RED",
            "ExpirationDateTime": "2009-08-16T16:55:43.577+02:00",
            "id": "38",
            "Message": "We are going down",
            "ModifiedDateTime": "2009-08-17T11:14:20.5+02:00",
            "MoreInfo": null 
        } 
    }
}

On the client side I'm using JQuery to get the result using the ajax function like this:

$.ajax({
   type: "POST",
   url: "TickerFeeder.asmx/GetTicket",
   data: "{}",
   contentType: "application/json; charset=utf-8",
   dataType: "json",
   success: function(resultJSON) {
     //-- Please fill your code here for getting the first item from the array into variables
   }

But I'm missing out the stuff how to retrieve the first item from the JSON array into some variables. Something like this (pseudo-code):

var message = resultJSON[0].Message
var cssclass = resultJSON[0].CssClass

Anybody with a hint,help?

Thanks for your help Cheers Frank

Upvotes: 1

Views: 2725

Answers (2)

user155814
user155814

Reputation: 101

Ok, found out that my Asp.Net webService was producing a wrong result set. So instead of returning a string item I returned a complete object and handled the Json conversion to Asp.Net webservice. That did the trick !

Upvotes: 0

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827366

Your JSON is not valid, you should use quotes on the MyResult and Ticket members.

{
    "MyResult": {
        "Ticket": {
            "Author": "rd",
            "CssClass": "RED",
            "ExpirationDateTime": "2009-08-16T16:55:43.577+02:00",
            "id": "38",
            "Message": "We are going down",
            "ModifiedDateTime": "2009-08-17T11:14:20.5+02:00",
            "MoreInfo": null 
        } 
    }
}

Also there is no array involved, the arrays are defined with the square bracket characters [....] literal notation, so you can access your values directly:

resultJSON.MyResult.Ticket.Message;
resultJSON.MyResult.Ticket.CssClass;

Upvotes: 9

Related Questions