Solved

Accessing data in JSON response.d

Posted on 2016-11-16
4
30 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 51

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 51

Expert Comment

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now