0

I'm trying to pass a list of json Object in an array using javascript. Here is the list:

[{"_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

mmuzahid
  • 2,252
  • 23
  • 42
Gunesh.John
  • 95
  • 2
  • 8
  • What is expected result ? – guest271314 Mar 27 '16 at 06:09
  • To save all the values in the json list in to array. eg: _id[i]="1"; age[i]="10"; name[i]="carlos" – Gunesh.John Mar 27 '16 at 06:10
  • 1
    `arrayResults.files.length` should be `jsonData.files.length` and `jsonData.files.age[i]` according to your current structure should be `jsonData.files[i].json.age` – photoionized Mar 27 '16 at 06:11
  • @Gunesh.John: Why do you want to do that? It's generally a bad idea to work with a set of parallel arrays. It leads to complicated and error-prone code. The original structure of your JSON data is better and easier to work with, because all the data for a single person is grouped together. – Michael Geary Mar 27 '16 at 06:11
  • The json output is generated from jsonstore in mobilefirst. I need to go through arrays to manipulate a particular data. – Gunesh.John Mar 27 '16 at 06:13
  • Does converting the data to individual arrays gain you any advantage? It just seems like extra complication. It's easy to work with the data as it is, as photoionized's example shows. `jsonData.files[i]` gives you direct access to all the data for an individual person. – Michael Geary Mar 27 '16 at 06:15

2 Answers2

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.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
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);
}
Moumit
  • 8,314
  • 9
  • 55
  • 59