This question already has an answer here:
这个问题在这里已有答案:
- Access / process (nested) objects, arrays or JSON 20 answers
访问/处理(嵌套)对象,数组或JSON 20答案
I'm trying to pass a list of json Object in an array using javascript. Here is the list:
我正在尝试使用javascript传递数组中的json对象列表。这是清单:
[{"_id":1,"json":{"age":10,"name":"carlos"}},{"_id":2,"json":{"age":10,"name":"carlos"}}]
Here is the code:
这是代码:
var arrayResults = '{"files":' + '[{"_id":1,"json":{"age":10,"name":"carlos"}},{"_id":2,"json":{"age":10,"name":"carlos"}}]}';
alert (arrayResults.files.length);
var jsonData = JSON.parse(arrayResults);
for (var i = 0; i < arrayResults.files.length; i++) {
var file = jsonData.files.age[i];
alert(file);
}
Can I have some help please? Thanks
我可以帮忙吗?谢谢
2 个解决方案
#1
0
It's not exactly clear what you are looking for, but the following script does what I think you want, which is to iterate through the pieces of your data and alert the interesting facts about them:
目前还不完全清楚你在寻找什么,但是下面的脚本做了我认为你想要的东西,即迭代你的数据并提醒他们有趣的事实:
var arrayResults = '{"files":' + '[{"_id":1,"json":{"age":10,"name":"carlos"}},{"_id":2,"json":{"age":10,"name":"carlos"}}]}';
var jsonData = JSON.parse(arrayResults);
alert (jsonData.files.length);
for (var i = 0; i < jsonData.files.length; i++) {
var file = jsonData.files[i];
alert(file.json.name + " " + file._id + " is " + file.json.age);
}
There were a number of things wrong with your posted code; rather than enumerating all of the problems, perhaps you can glean from this answer enough to help you continue your work.
您发布的代码有很多问题;而不是列举所有问题,也许你可以从这个答案收集到足以帮助你继续工作。
#2
0
var arrayResults = '{"files":' + '[{"_id":1,"json":{"age":10,"name":"carlos"}},{"_id":2,"json":{"age":10,"name":"carlos"}}]}';
//alert (arrayResults.files.length);
var jsonData = JSON.parse(arrayResults);
alert (jsonData.files.length);
for (var i = 0; i < jsonData.files.length; i++) {
var file = jsonData.files[i];
alert(file.json.age);
//Don't know where counter_name is defined
//alert(file.counter_name);
}
#1
0
It's not exactly clear what you are looking for, but the following script does what I think you want, which is to iterate through the pieces of your data and alert the interesting facts about them:
目前还不完全清楚你在寻找什么,但是下面的脚本做了我认为你想要的东西,即迭代你的数据并提醒他们有趣的事实:
var arrayResults = '{"files":' + '[{"_id":1,"json":{"age":10,"name":"carlos"}},{"_id":2,"json":{"age":10,"name":"carlos"}}]}';
var jsonData = JSON.parse(arrayResults);
alert (jsonData.files.length);
for (var i = 0; i < jsonData.files.length; i++) {
var file = jsonData.files[i];
alert(file.json.name + " " + file._id + " is " + file.json.age);
}
There were a number of things wrong with your posted code; rather than enumerating all of the problems, perhaps you can glean from this answer enough to help you continue your work.
您发布的代码有很多问题;而不是列举所有问题,也许你可以从这个答案收集到足以帮助你继续工作。
#2
0
var arrayResults = '{"files":' + '[{"_id":1,"json":{"age":10,"name":"carlos"}},{"_id":2,"json":{"age":10,"name":"carlos"}}]}';
//alert (arrayResults.files.length);
var jsonData = JSON.parse(arrayResults);
alert (jsonData.files.length);
for (var i = 0; i < jsonData.files.length; i++) {
var file = jsonData.files[i];
alert(file.json.age);
//Don't know where counter_name is defined
//alert(file.counter_name);
}