-1

I am trying to parse this json data

[
    {   "title":"Yorkshire 199/8 * v Durham 237/10", 
        "link":"http://www.cricinfo.com/ci/engine/match/693421.html?CMP=OTC-RSS", 
        "description":"Yorkshire 199/8 * v Durham 237/10", 
        "guid":"http://www.cricinfo.com/ci/engine/match/693421.html"},
    {
        "title":"Essex v Warwickshire 271/7 *", 
        "link":"http://www.cricinfo.com/ci/engine/match/693423.html?CMP=OTC-RSS", 
        "description":"Essex v Warwickshire 271/7 *", 
        "guid":"http://www.cricinfo.com/ci/engine/match/693423.html"},
    {
        "title":"Singapore v Malaysia", 
        "link":"http://www.cricinfo.com/ci/engine/match/774365.html?CMP=OTC-RSS", 
        "description":"Singapore v Malaysia", 
        "guid":"http://www.cricinfo.com/ci/engine/match/774365.html"}
]

returning from server side method and using this method to iterate through each item

$.ajax({
               type: "POST",
               url: "Default.aspx/ServerSideMethod",
               //data: JSON.stringify({ 'p': 'Sent Text' }),
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               async: true,
               cache: false,
               success: function (data) {
                   //alert(data.d) //returns the result set displayed above.
                   $.each(data, function (i, obj) {
                       alert(obj.title); // returns undefined.

                   });
               },
               error: function (x, e) { alert(x.responseText); }
           })

but it always return undefined. Is there any issue with my jquery function or with data? My server side function is working fine and the above Jquery function also show the returned values but I am unable to parse this value.

I also tried this thread to fix this issue but nothing succeeds. Any one please...

Community
  • 1
  • 1
Lali
  • 2,816
  • 4
  • 30
  • 47

1 Answers1

0

You are iterating over the wrong object:

$.ajax({
           type: "POST",
           url: "Default.aspx/ServerSideMethod",
           //data: JSON.stringify({ 'p': 'Sent Text' }),
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           async: true,
           cache: false,
           success: function (data) {
               //alert(data.d) //returns the result set displayed above.
               $.each(data.d, function (i, obj) { // <---- use data.d here, not data
                   alert(obj.title);

               });
           },
           error: function (x, e) { alert(x.responseText); }
       })

When you just iterate over data, obj takes the values of each property of data. So, for example, at some point, obj is data.d. You then try to alert obj.title, which is data.d.title and doesn't exist.

By iterating over data.d, you set obj to be, for example, data.d[0]. Then,

obj.title === data.d[0].title
          === "Yorkshire 199/8 * v Durham 237/10"`
elixenide
  • 44,308
  • 16
  • 74
  • 100