JavaScript问题中的json解析 [英] json parsing in JavaScript issue

查看:147
本文介绍了JavaScript问题中的json解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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']

              }
})
          }

可能是什么问题?

推荐答案

无需使用JSON.parse()进行解析,因为答案本身已经是对象,您可以执行此操作.

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>

其背后的原因是因为JSON已经是一个对象.因此,不需要JSON.parse()或JSON.stringify().就那么简单!希望这会有所帮助!

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循环中,im遍历对象.并不断重写变量a.因此,最后变量a将等于"Ok",因为那是JSON中的最后一个对象".现在假设您要保留每个对象",那么您将需要一个计数":

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++;
  }  

现在,实际JSON拥有该JSON的所有对象".

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

这篇关于JavaScript问题中的json解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆