I have an Android Application and a Server with Node.js that uses Restify.js and MongoJS to store data from and to my android application.
For now, I am using JSONArray from Android (but in my case, it's json-simple lib based on original lib) and I have a JSONArray containing JSONObjects (from json-simple lib as well).
In my case, a JSONObject is as following :
{
"PLACE_NAME":"Paris",
"COLOR_ID":"2131099684",
"LIST_DATES":["2014-05-23","2014-05-22","2014-05-21"]
}
My point here, I have many many JSONObjects that obviously respect the same architecture : a place name, a color ID and list of date(s).
Afterwards, I am storing this JSONObjects in my JSONArray. Simple like that, in a loop, for as many JSONOBject I have :
myJsonArray.add(myJsonObj);
Hence, the content of my JSONArray is as following :
[
{
"PLACE_NAME":"Paris",
"COLOR_ID":"2131099684",
"LIST_DATES":["2014-05-23","2014-05-22","2014-05-21"]
},
{
"PLACE_NAME":"Milan",
"COLOR_ID":"2131099667",
"LIST_DATES":["2014-05-14","2014-05-16","2014-05-15"]
}
// ... and it goes on and on
]
So far that data architecture worked very well because I can store in a file and thanks to JSONArray from json-simple lib, I can use a built-in parser that can easily parse the file.
THerefore, when I want to retrieve all the JSONObjects from the JSONArray stored in the file, it is a s simple as that :
final FileReader fr = new FileReader(homeActivitySavesPath.getAbsolutePath());
final JSONParser parser = new JSONParser();
this.jsonArray = (JSONArray) parser.parse(fr);
if (this.jsonArray.size() >= 1) {
for (int i = 0; i < this.jsonArray.size(); i++) {
final JSONObject jsonObject = (JSONObject)this.jsonArray.get(i);
// ... doing some logical code to restore data
}
}
As I am completely new in JavaScript I am having a hard time to parse this kind of JSONArray.
To start to understand how to parse, I print the content of the POST request I am sending via an HTTP connection from the Android Application : '
console.log("content of request params -> %s", request.params);
And I get that :
content of request params -> [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Clearly a
[object Object]
is the aforementioned JSONObject right ?
Sor how would you built a loop in JavaScript capable for looping through this kind of JSONArray ?
I would like to store each JSONObject separatly in my MongoDB collection.
Thanks for any help !