Solved

Accessing data in JSON response.d

Posted on 2016-11-16
4
70 Views
Last Modified: 2016-11-16
I think after 50 rounds back to google and an unending set of error responses I need help.  I am successfully generating a JSON response (I can see it in Firebug) but cant access the underlying data.  The JSON response is

{"d":[{"__type":"CurrentTeams","CurrentTeams":756,"TargetTeams":688,"RangeMax":1200,"TickInterval":200
,"Tick1":300,"Tick2":600,"Tick3":900}]}

Open in new window


My javascript to make the request is
  $(document).ready(function () {


                    $(function () {
                        $.ajax({
                            type: "POST",
                            url: "../services/seasons_svc.aspx/LoadCurrentSeasonsWithTarget",
                            data: '{}',
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: OnSuccess,
                            failure: function (response) {
                                alert(response.responseText);
                            },
                            error: function (response) {
                                alert(response.responseText);
                            }
                        });
                    });

                    function OnSuccess(response) {

                        var xmlDoc = $.parseXML(response.d);
                        var xml = $(xmlDoc);
                        var customers = xml.find("d");
                     
                        alert($(xmlDoc));

Open in new window


I just need to be able to get access to the individual items in the response, for example
var tTeams = response.d("TargetTeams"); (which doesn't work)

Any advice or input would be greatly appreciated.
0
Comment
Question by:clintnash
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 41890110
I am assuming here that response contains only the JSON string returned as per your question.

 function OnSuccess(response) {
  var j = JSON.parse(response);
  //access items like this
  if (j.CurrentTeams > j.TargetTeams) alert('Target reached!');
}

Open in new window

0
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41890130
You don't need to Parse the JSON - JQuery does that for you with the dataType: "JSON" property.
function OnSuccess(response) {
  alert(response.d[0].TargetTeams);
}

Open in new window


The response is JSON (which is a javascript object) - properly formatted it looks like this
{
   "d":[
      {
         "__type":"CurrentTeams",
         "CurrentTeams":756,
         "TargetTeams":688,
         "RangeMax":1200,
         "TickInterval":200,
         "Tick1":300,
         "Tick2":600,
         "Tick3":900
      }
   ]
}

Open in new window

You can therefore use object notation to access the properties in the returned object.

Note: Although AJAX stands for Asynchronous Javascript and XML - we rarely see XML being returned - JSON is the standard. Your return is JSON and you have told jQuery to interpret it as such - therefore in your success() function response will be a parsed JSON object.
0
 
LVL 1

Author Closing Comment

by:clintnash
ID: 41890252
Thank you, in my attempts I missed the index of d. This nailed it.
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41890401
You are welcome.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question