views:

597

answers:

3

Hi, 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

+7  A: 

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;
CMS
A: 

Hm, I have changed the output from the webservice so the response is:

{"d":"{ \"Ticker\" : { \"Message\" : \"We are going down\", \"Color\" : \"RED\"} }"}

and the JSON result is

"{ "Ticker" : { "Message" : "We are going down", "Color" : "RED"} }"

The Headere looks like this:

Response-headere
Server  ASP.NET Development Server/9.0.0.0
Date    Wed, 19 Aug 2009 08:12:56 GMT
X-AspNet-Version    2.0.50727
Cache-Control   private, max-age=0
Content-Type    application/json; charset=utf-8
Content-Length  84
Connection  Close
Request-headere
Host    localhost:2528
User-Agent  Mozilla/5.0 (Windows; U; Windows NT 5.1; da; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2
Accept  application/json, text/javascript, */*
Accept-Language da
Accept-Encoding gzip,deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive  300
Connection  keep-alive
Content-Type    application/json; charset=utf-8
X-Requested-With    XMLHttpRequest
Referer http://localhost:2528/WebInfoBoard/Newsboard/ticketfeed.aspx
Content-Length  2
Cookie  tickerMessage=%7B%20%22Ticker%22%20%3A%20%7B%20%22Message%22%20%3A%20%22We%20are%20going%20down%22%2C%20%22Color%22%20%3A%20%22RED%22%7D%20%7D
Pragma  no-cache
Cache-Control   no-cache

But still im unable to retrieve the result using:

alert(resultJSON.Ticker.Message);

Still apprecitiating any help

thanks

Solved !The asp.net webservice changed from returning a self-made json string to returning a object. The webservice would then transform the object to valid json and that solved the problem
A: 

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 !