简体   繁体   English

使用带有 javascript 的对象的 JSON 数组

[英]Use a JSON array with objects with javascript

I have a function that will get a JSON array with objects.我有一个 function 将获得一个带有对象的 JSON 数组。 In the function I will be able to loop through the array, access a property and use that property.在 function 中,我将能够遍历数组,访问属性并使用该属性。 Like this:像这样:

Variable that I will pass to the function will look like this:我将传递给 function 的变量将如下所示:

[{
  "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?有什么建议可以解决吗?

This isn't a single JSON object.这不是单个 JSON 对象。 You have an array of JSON objects.您有一组 JSON 对象。 You need to loop over array first and then access each object.您需要先遍历数组,然后访问每个对象。 Maybe the following kickoff example is helpful:也许以下启动示例会有所帮助:

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

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);
// ...

To learn more about JSON, check MDN web docs: Working with JSON .要了解有关 JSON 的更多信息,请查看MDN 网络文档:使用 JSON

This is your dataArray :这是你的dataArray

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

Then parseJson can be used:然后可以使用parseJson

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

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

After that you just iterate over the array as normal.之后,您只需像往常一样遍历数组。

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

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

@Swapnil Godambe It works for me if JSON.stringfy is removed. @Swapnil Godambe 如果 JSON.stringfy 被删除,它对我有用。 That is:那是:

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

 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>");

How do I get all id's from an Array of json objects (myarray) where age = "43"?如何从年龄 =“43”的 json 对象(myarray)数组中获取所有 ID?

const myarray = [ {"id":"1", "age":"33"}, {"id":"2", "age":"43"}, {"id":"3", "age":"43"}, {"id":"4", "age":"53"} ]; const myarray = [ {"id":"1", "age":"33"}, {"id":"2", "age":"43"}, {"id":"3", "年龄":"43"}, {"id":"4", "age":"53"} ];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM