使用带有 javascript 对象的 JSON 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2799283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:58:11  来源:igfitidea点击:

Use a JSON array with objects with javascript

javascriptjson

提问by Fredrik Johansson

I have a function that will get a JSON array with objects. In the function I will be able to loop through the array, access a property and use that property. Like this:

我有一个函数可以获取一个带有对象的 JSON 数组。在函数中,我将能够遍历数组,访问一个属性并使用该属性。像这样:

Variable that I will pass to the function will look like this:

我将传递给函数的变量如下所示:

[{
  "id": 28,
  "Title": "Sweden"
}, {
  "id": 56,
  "Title": "USA"
}, {
  "id": 89,
  "Title": "England"
}]

function test(myJSON) {
  // maybe parse my the JSON variable?
  // and then I want to loop through it and access my IDs and my titles
}

Any suggestions how I can solve it?

任何建议我如何解决它?

回答by BalusC

This isn't a single JSON object. You have an array of JSON objects. You need to loop over array first and then access each object. Maybe the following kickoff example is helpful:

这不是单个 JSON 对象。您有一组 JSON 对象。您需要先遍历数组,然后访问每个对象。也许以下启动示例会有所帮助:

var arrayOfObjects = [{
  "id": 28,
  "Title": "Sweden"
}, {
  "id": 56,
  "Title": "USA"
}, {
  "id": 89,
  "Title": "England"
}];

for (var i = 0; i < arrayOfObjects.length; i++) {
  var object = arrayOfObjects[i];
  for (var property in object) {
    alert('item ' + i + ': ' + property + '=' + object[property]);
  }
  // If property names are known beforehand, you can also just do e.g.
  // alert(object.id + ',' + object.Title);
}

To learn more about JSON, check this article.

要了解有关 JSON 的更多信息,请查看这篇文章

Update: if the array of JSON objects is actually passed in as a plain vanilla string, then you would indeed need eval()here.

更新:如果 JSON 对象数组实际上是作为普通字符串传入的,那么您确实需要eval()在这里。

var string = '[{"id":28,"Title":"Sweden"}, {"id":56,"Title":"USA"}, {"id":89,"Title":"England"}]';
var arrayOfObjects = eval(string);
// ...

回答by Swapnil Godambe

This is your dataArray:

这是你的dataArray

[
   {
      "id":28,
      "Title":"Sweden"
   },
   {
      "id":56,
      "Title":"USA"
   },
   {
      "id":89,
      "Title":"England"
   }
]

Then parseJsoncan be used:

然后parseJson可以使用:

$(jQuery.parseJSON(JSON.stringify(dataArray))).each(function() {  
    var ID = this.id;
    var TITLE = this.Title;
});

回答by Sean Kinsey

By 'JSON array containing objects' I guess you mean a string containing JSON?

通过“包含对象的 JSON 数组”,我猜你的意思是一个包含 JSON 的字符串?

If so you can use the safe var myArray = JSON.parse(myJSON)method (either native or included using JSON2), or the usafe var myArray = eval("(" + myJSON + ")"). eval should normally be avoided, but if you are certain that the content is safe, then there is no problem.

如果是这样,您可以使用安全var myArray = JSON.parse(myJSON)方法(本机方法或使用JSON2包含)或 usafe var myArray = eval("(" + myJSON + ")")。通常应该避免使用 eval,但是如果您确定内容是安全的,那么就没有问题。

After that you just iterate over the array as normal.

之后,您只需像往常一样遍历数组。

for (var i = 0; i < myArray.length; i++) {
    alert(myArray[i].Title);
}

回答by Martyn

Your question feels a little incomplete, but I think what you're looking for is a way of making your JSON accessible to your code:

您的问题感觉有点不完整,但我认为您正在寻找的是一种使您的代码可以访问您的 JSON 的方法:

if you have the JSON string as above then you'd just need to do this

如果你有上面的 JSON 字符串,那么你只需要这样做

var jsonObj = eval('[{"id":28,"Title":"Sweden"}, {"id":56,"Title":"USA"}, {"id":89,"Title":"England"}]');

then you can access these vars with something like jsonObj[0].id etc

然后您可以使用类似 jsonObj[0].id 等内容访问这些变量

Let me know if that's not what you were getting at and I'll try to help.

如果这不是您想要的,请告诉我,我会尽力提供帮助。

M

回答by shaosh

@Swapnil Godambe It works for me if JSON.stringfy is removed. That is:

@Swapnil Godambe 如果 JSON.stringfy 被删除,它对我有用。那是:

$(jQuery.parseJSON(dataArray)).each(function() {  
    var ID = this.id;
    var TITLE = this.Title;
});

回答by Jamhari

var datas = [{"id":28,"Title":"Sweden"}, {"id":56,"Title":"USA"}, {"id":89,"Title":"England"}];
document.writeln("<table border = '1' width = 100 >");
document.writeln("<tr><td>No Id</td><td>Title</td></tr>"); 
for(var i=0;i<datas.length;i++){
document.writeln("<tr><td>"+datas[i].id+"</td><td>"+datas[i].Title+"</td></tr>");
}
document.writeln("</table>");