gpt4 book ai didi

JavaScript Json 解析器

转载 作者:行者123 更新时间:2023-12-02 17:55:00 25 4
gpt4 key购买 nike

我正在尝试仅使用 javascript 编写 JSON 解析器。我只想使用 javascript 的原因是我想解析从 Meteor 服务器端的 API 返回的结果。但是,我在解析它时遇到了麻烦。下面是返回的 JSON:

{
"R_5N4205x1hhF6pGZ":{
"ResponseSet":"Default Response Set",
"Name":"Anonymous",
"ExternalDataReference":"",
"EmailAddress":"",
"Status":"16",
"StartDate":"2014-01-09 11:19:51",
"EndDate":"2014-01-09 11:19:56",
"Finished":"1",
"Q1":"Hello "
},
"R_7mqYPn4rZfNtVif":{
"ResponseSet":"Default Response Set",
"Name":"Anonymous",
"ExternalDataReference":"",
"EmailAddress":"",
"Status":"16",
"StartDate":"2014-01-09 11:21:58",
"EndDate":"2014-01-09 11:22:05",
"Finished":"1",
"Q1":"Name identifier"
},
"R_bHs2h06HSQ1h2Qh":{
"ResponseSet":"Default Response Set",
"Name":"Anonymous",
"ExternalDataReference":"",
"EmailAddress":"",
"Status":"16",
"StartDate":"2014-01-09 11:28:24",
"EndDate":"2014-01-09 11:28:30",
"Finished":"1",
"Q1":"Test"
},
"R_20rlmxxgGkYnWWF":{
"ResponseSet":"Default Response Set",
"Name":"Some, Name",
"ExternalDataReference":"",
"EmailAddress":"",
"Status":"0",
"StartDate":"2014-01-09 12:21:15",
"EndDate":"2014-01-09 12:21:27",
"Finished":"1",
"Q1":"hjjhhjhj"
},
"R_e36yuRbnMmh38dD":{
"ResponseSet":"Default Response Set",
"Name":"Anonymous",
"ExternalDataReference":"",
"EmailAddress":"",
"IPAddress":"161.185.153.4",
"Status":"0",
"StartDate":"2014-01-09 12:23:04",
"EndDate":"2014-01-09 12:23:11",
"Finished":"1",
"Q1":"hello world "
}
}

我想使用对象父键作为键“responseID”保存每个响应对象:值(即array[“responseID”] =“R_e36yuRbnMmh38dD”)

  "R_e36yuRbnMmh38dD":{
"ResponseSet":"Default Response Set",
"Name":"Anonymous",
"ExternalDataReference":"",
"EmailAddress":"",
"IPAddress":"161.185.153.4",
"Status":"0",
"StartDate":"2014-01-09 12:23:04",
"EndDate":"2014-01-09 12:23:11",
"Finished":"1",
"Q1":"hello world "
}

我想将结果集保存在 mongoDb 中。

最佳答案

您可以使用 NodeJS JSON.parse() 函数,但如果我理解您想要更多地使用对象中的键创建一个对象。

这是代码,如果这是您想要做的:

var originalObject;
var resultArray;
for (var key in originalObject) {
if(originalObject.hasOwnProperty(key )){
var obj = originalObject[key];
obj.responseID = key;
resultArray.push(obj);
}
}

但是你的问题有点令人困惑。对于 mongoDB,我建议 nodejs native driver

关于JavaScript Json 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21032923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com