如何解析具有多个值的json数据?

Posted

技术标签:

【中文标题】如何解析具有多个值的json数据?【英文标题】:how to parse a json data with multiple values? 【发布时间】:2018-12-06 00:23:01 【问题描述】:

我有一个多维 json 。我想解析它以获取值。

var json='"Links":[],"RequestedObject":"FieldContents":"21514":"Type":1,"IsError":false,"Value":"Saneen","FieldId":21514,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21516":"Type":1,"IsError":false,"Value":"English","FieldId":21516,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21517":"Type":1,"IsError":false,"Value":"Malayalam","FieldId":21517,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21515":"Type":2,"IsError":false,"Value":26.0,"FieldId":21515,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21518":"Type":2,"IsError":false,"Value":80.0,"FieldId":21518,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21519":"Type":2,"IsError":false,"Value":40.0,"FieldId":21519,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21520":"Type":4,"IsError":false,"Value":"ValuesListIds":[72639],"OtherText":null,"FieldId":21520,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21523":"Type":3,"IsError":false,"Value":"2017-03-29T00:00:00","FieldId":21523,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21510":"Type":6,"FieldId":21510,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21511":"Type":21,"FieldId":21511,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21512":"Type":22,"FieldId":21512,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21521":"Type":11,"Value":null,"FieldId":21521,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"IsSuccessful":true,"ValidationMessages":[]';

我尝试过 JSON.parse ,但没​​有运气。

【问题讨论】:

JSON.parse(json) JSON.parse 在这里工作得很好。 JSON.parse 应该能够做到这一点。你用的是什么浏览器? Safely turning a JSON string into an object的可能重复 【参考方案1】:

这是JSON.parse()和here is document for that的简单示例:

var json='"Links":[],"RequestedObject":"FieldContents":"21514":"Type":1,"IsError":false,"Value":"Saneen","FieldId":21514,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21516":"Type":1,"IsError":false,"Value":"English","FieldId":21516,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21517":"Type":1,"IsError":false,"Value":"Malayalam","FieldId":21517,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21515":"Type":2,"IsError":false,"Value":26.0,"FieldId":21515,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21518":"Type":2,"IsError":false,"Value":80.0,"FieldId":21518,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21519":"Type":2,"IsError":false,"Value":40.0,"FieldId":21519,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21520":"Type":4,"IsError":false,"Value":"ValuesListIds":[72639],"OtherText":null,"FieldId":21520,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21523":"Type":3,"IsError":false,"Value":"2017-03-29T00:00:00","FieldId":21523,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21510":"Type":6,"FieldId":21510,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21511":"Type":21,"FieldId":21511,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21512":"Type":22,"FieldId":21512,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"21521":"Type":11,"Value":null,"FieldId":21521,"UpdateInformation":"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null,"IsSuccessful":true,"ValidationMessages":[]';

var obj = JSON.parse(json);
console.log(obj);

// retrieve particular fields

var newFieldContents = ;

for(var key in obj['RequestedObject']['FieldContents'])
  var o = obj['RequestedObject']['FieldContents'][key];
  newFieldContents[key] = 
    Value: o['Value'],
    FieldId: o['FieldId']
  


console.log(newFieldContents);

更新

添加如何检索特定字段。

【讨论】:

我只需要一些特定的字段,例如 value 和 FieldId 我已更新,但并非所有对象都包含值。

以上是关于如何解析具有多个值的json数据?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用pyspark将具有多个可能值的Json数组列表转换为数据框中的列

如何使用 Swifty 动态创建具有多个键和值的 json 对象

如何展平具有大量子值的 Json 数据

Jquery从具有数组/ JSON值的多个数据属性中检索数据

如何在Java中解析具有空值的JSON对象?

Phpmyadmin 仅接受来自具有多个输入值的 json 数组中的一个条目