1

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

2 Answers 2

9

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;
Sign up to request clarification or add additional context in comments.

Comments

0

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 !

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.