Solved

Accessing data in JSON response.d

Posted on 2016-11-16
4
89 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 55

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 55

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

809 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