Solved

Accessing data in JSON response.d

Posted on 2016-11-16
4
114 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 56

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 56

Expert Comment

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

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

756 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