dqifn68206
2018-01-29 14:44
浏览 43

如何在Javascript中读取值或反序列化JSON结构?

from an associative and multidimensional Array in PHP i get this object by json_encode():

{"2047":
  {"f1a":
   {"val":["7","7","6","1","6","6"],
    "cnt":6,
    "sum":33,
    "avg":5.5
   },
  {"f2a":
   {"val":["7","6","5","7","5","3","5","7","6","7"],
    "cnt":10,
    "sum":58,
    "avg":5.8
   },
  "2048": ...
}

and i want to read the value of avg of f2a and 2047.

图片转代码服务由CSDN问答提供 功能建议

来自PHP中的关联和多维数组我通过json_encode()获取此对象: \ n

  {“2047”:
 {“f1a”:
 {“val”:[“7”,“7”,“6”,“1”,“6”,“6”  ],
“cnt”:6,
“sum”:33,
“avg”:5.5 
},
 {“f2a”:
 {“val”:[“7”,“6  “,”“5”,“7”,“5”,“3”,“5”,“7”,“6”,“7”],
“cnt”:10,
“sum”:58  ,
“avg”:5.8 
},
“2048”:... 
} 
   
 
 

我想读取avg的值 f2a和2047。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doutui2883 2018-01-29 14:59
    已采纳
      var myJson = {
      2047: {
        f1a: {
          val: [7, 7, 6, 1, 6, 6],
          cnt: 6,
          sum: 33,
          avg: 5.5
        },
          f2a: {
            val: [7, 6, 5, 7, 5, 3, 5, 7, 6, 7],
            cnt: 10,
            sum: 58,
            avg: 5.8
          }
      }
    };
    console.log(myJson['2047']['f1a']['avg']);
    
    打赏 评论
  • douzhuoxia0587 2018-01-29 15:03

    You can deserialize your JSON with JSON.parse (serialized)

    打赏 评论
  • dongyupen6269 2018-01-29 15:04

    You can use var obj = JSON.parse(str), then traverse the object using obj['2047']['f1a']

    打赏 评论

相关推荐 更多相似问题