Solved

Accessing data in JSON response.d

Posted on 2016-11-16
4
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 58

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 58

Expert Comment

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

Featured Post

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

617 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