-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?

7
  • what is key in json[key] Commented Mar 26, 2016 at 22:01
  • Is this because of the JSON.stringify? How can i parse it as json? Commented Mar 26, 2016 at 22:03
  • 2
    json is always a string Commented Mar 26, 2016 at 22:03
  • refer to this question: Parse JSON in JavaScript Commented Mar 26, 2016 at 22:07
  • 1
    Possible duplicate of SyntaxError: JSON.parse: unexpected character Commented Mar 26, 2016 at 22:24

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Only 'the person' would know; and there is no requirement to explain downvotes.. annoying as it may be.
But result1[1] should be statusText as documented at api.jquery.com/jquery.when. Why you write a = result1[key]?
@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?
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.
@AmanuelBogale I'm not sure if i understood the loop section. I got the following error: Uncaught ReferenceError: actualJSON is not defined.
|
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.

Comments

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'
}

1 Comment

replace json with result
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.

Comments

-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>

6 Comments

IIRC, the returned object is already deserialized. Using parse probably results in errors about "o".
@user2864740 yes I read that the OP is getting the error in the other answers comment
@Daedalus lets remove the noise here. As well as Reddy. Lets polish up this question and anwser a bit!
@Daedalus changd my answer.
Im saying that snippet is confusing. It gives a anwser i didnt expect.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.