我有一个 Android 应用程序和一个带有 Node.js 的服务器,它使用 Restify.js 和 MongoJS 在我的 android 应用程序中存储数据。
目前,我正在使用来自 Android 的 JSONArray(但在我的情况下,它是基于原始库的 json-simple 库)并且我有一个包含 JSONObjects 的 JSONArray(也来自 json-simple 库)。
在我的例子中,一个 JSONObject 如下:
{
"PLACE_NAME":"Paris",
"COLOR_ID":"2131099684",
"LIST_DATES":["2014-05-23","2014-05-22","2014-05-21"]
}
我的观点是,我有许多 JSONObjects 显然尊重相同的架构:地名、颜色 ID 和日期列表。
之后,我将这个 JSONObjects 存储在我的 JSONArray 中。像这样简单,在一个循环中,对于我拥有的尽可能多的 JSONOBject:
myJsonArray.add(myJsonObj);
因此,我的 JSONArray 的内容如下:
[
{
"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
]
到目前为止,数据架构运行良好,因为我可以存储在文件中,并且感谢 json-simple lib 中的 JSONArray,我可以使用可以轻松解析文件的内置解析器。
因此,当我想从存储在文件中的 JSONArray 中检索所有 JSONObjects 时,就这么简单:
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
}
}
由于我是 JavaScript 的新手,我很难解析这种 JSONArray。
为了开始了解如何解析,我打印了我通过来自 Android 应用程序的 HTTP 连接发送的 POST 请求的内容:'
console.log("请求参数的内容 -> %s", request.params);
我明白了:
请求参数的内容 -> [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
显然一个
[对象对象]
前面提到的 JSONObject 对吗?
或者,您将如何在 JavaScript 中构建一个能够遍历这种 JSONArray 的循环?
我想将每个 JSONObject 单独存储在我的 MongoDB 集合中。
谢谢你的帮助 !