-1

I have the following json:

{
"result":[
   {
     "a": 500000,
     "b": null,
     "c": 0,
     "d": 0,
     "e": 1855
  },
  {
     "a": 500001,
     "b": null,
     "c": 0,
     "d": 0,
     "e": 3770
  }
],
"host": "43252ed565f9",
"time": "Wed Mar 23 09:57:43 UTC 2016",
"status": "Ok"
}

I'm trying to parse it, but got a strange value of "[", instead each one of the keys.

    function f() {
              var request1 = $.ajax({
                  url: '/a', 
                  type: 'GET',
              });
              var request2 = $.ajax({
                   url : '/b',
                   type: "GET"
              });
              $.when(request1, request2).done(function(result1, result2){
                        //result1 = [Object, "success", Object]
                        json =  JSON.stringify(result1)

              for( key in json){
                    myJson = json[key] //The value is: "["

               a = myJson['a']
               b = myJson['b']
               c = myJson['c']
               d = myJson['d']
               e = myJson['e']

              }
})
          }

What could be the issue?

Omri
  • 1,436
  • 7
  • 31
  • 61

5 Answers5

3

No need to parse by JSON.parse() because the answer is already an object itself you can do this.

function f() {
              var request1 = $.ajax({
                  url: '/a', 
                  type: 'GET',
              });
              var request2 = $.ajax({
                   url : '/b',
                   type: "GET"
              });
              $.when(request1, request2).done(function(result1, result2){
                        //result1 = [Object, "success", Object]


              for( key in result1){
                    a = result1[key] //The value is: "["

              }
})
          }

In fact ill show a snippet:

var Json = {
  "result": [{
    "a": 500000,
    "b": null,
    "c": 0,
    "d": 0,
    "e": 1855
  }, {
    "a": 500001,
    "b": null,
    "c": 0,
    "d": 0,
    "e": 3770
  }],
  "host": "43252ed565f9",
  "time": "Wed Mar 23 09:57:43 UTC 2016",
  "status": "Ok"
}

for (key in Json) {
  a = Json[key] //The value is: "["

}
document.getElementById("result").innerHTML = a
<p id="result"></p>

Reason Behind This

The reason behind this is because the JSON is already an object. Therfore there is no need to JSON.parse() it or JSON.stringify() it. Simple as that! Hope this helped!

FOR LOOP

In the for loop im looping over the objects.. And constantly rewritting the variable a. So at the end the variable a would equal "Ok", because thats the last "Object" in the JSON. Now lets say you want to keep every "Objects", then you will need a "count":

count=0; //GLOBAL VARIABLE.
  for ( key in result1){
    actualJSON[count]
    count++;
  }  

Now the actualJSON Has the all of the "objects" of that JSON.

Ultron
  • 21
  • 4
amanuel2
  • 4,508
  • 4
  • 36
  • 67
  • Only 'the person' would know; and there is no requirement to explain downvotes.. annoying as it may be. – user2864740 Mar 26 '16 at 22:12
  • But result1[1] should be statusText as documented at https://api.jquery.com/jquery.when/. Why you write `a = result1[key]`? – conventi Mar 26 '16 at 22:26
  • @AmanuelBogale I din't understand your `for` loop. Can you explain and add more details regarding how to extract each key and key into a parameter? – Omri Mar 26 '16 at 22:35
  • I'm referring to the last example in that page where is write: `Each argument is an array with the following structure: [ data, statusText, jqXHR ]`. So he must take the first item of the array? I do not want to be right and i'm sorry if i misunderstood your code. – conventi Mar 26 '16 at 22:40
  • @AmanuelBogale I'm not sure if i understood the loop section. I got the following error: `Uncaught ReferenceError: actualJSON is not defined`. – Omri Mar 27 '16 at 06:07
  • Well, that's was I've finally managed to to extract the parameters: `result1[0].result[0].a`, `result1[0].result[0].b`, `result1[0].result[1].a`, `result1[0].result[1].b`, etc. – Omri Mar 27 '16 at 08:11
1

Result1 is [Object, "success", Object] and not the JSON as described in final example here https://api.jquery.com/jquery.when/.

result1[0] should be the response of the ajax request and so it should be the javascript object that map the JSON of the response.

conventi
  • 430
  • 3
  • 8
1

result1 and result2 is already the objects, you don't need to parse or stringify it.

Just iterate it:

for(var key in result1){
    var a = result1[key];
    // do something with 'a'
}
isvforall
  • 8,768
  • 6
  • 35
  • 50
0

JSON.stringify returns a string.

Use:

var json = typeof(result1[0]) === 'string' ? JSON.parse(result1[0]) : result1[0];

The [0] is because $.ajax() returns the data in the first argument in done function.

JoniJnm
  • 720
  • 2
  • 10
  • 19
-1

After reading all the comments I understood that your result1 is an object. Hence no need to parse it, you can directly access the values. Here is a example snippet.

var Json = {
  "result": [{
    "a": 500000,
    "b": null,
    "c": 0,
    "d": 0,
    "e": 1855
  }, {
    "a": 500001,
    "b": null,
    "c": 0,
    "d": 0,
    "e": 3770
  }],
  "host": "43252ed565f9",
  "time": "Wed Mar 23 09:57:43 UTC 2016",
  "status": "Ok"
}

for (key in Json) {
  a = Json[key] 
  document.getElementById("result").innerHTML += a + '<br/>';
}
<p id="result"></p>
Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59