admin管理员组

文章数量:1415881

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

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

Share Improve this question edited Feb 8, 2011 at 16:49 Josh Lee 178k39 gold badges277 silver badges281 bronze badges asked Aug 18, 2009 at 7:35 user155814user155814 1011 silver badge9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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;

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

本文标签: javascriptParsing JSON array in JQueryStack Overflow