我一直在尝试解析从API获取的JSON。它似乎不起作用。当我尝试将整个响应传递到对象或数组中时,它将整个响应插入到单个对象或数组中。我只是发布API响应,而不是我尝试的代码:
{"canonical_url": "http://magento2-demo.nexcess.net/"}{"shop_properties": {"is_hypernode": false, "hypernode_without_magento": false, "loadtime_milliseconds": 638}}{"admin_url": null}{"security.supee5344": {"magento2": false, "resultString": "backend url required", "riskRating": "low", "result": "unkn", "magento1": true, "cloudflareCacheHit": null}}{"security.supee5994": {"magento2": false, "resultString": "installed", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.ransomware": {"magento2": true, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.cart2quoterce": {"magento2": false, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.ajaxcartrce": {"magento2": false, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.opendev": {"magento2": true, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.openversioncontrol": {"magento2": true, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.sslcheck": {"magento2": true, "resultString": "misconfigured", "riskRating": "medium", "result": "fail", "magento1": true, "cloudflareCacheHit": null}}{"security.guruincinfection": {"magento2": true, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.creditcardhijack": {"magento2": true, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.visbot": {"magento2": false, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.cryptojacking": {"magento2": true, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.outdatedserverversion": {"magento2": true, "resultString": "unknown", "riskRating": "low", "result": "unkn", "magento1": true, "cloudflareCacheHit": null}}{"security.amastyopendownload": {"magento2": false, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.cacheleak": {"magento2": true, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.openmagmi": {"magento2": false, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.webforms": {"magento2": false, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.bruteforce": {"magento2": false, "resultString": "protected", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.magversion": {"magento2": true, "resultString": "CE 2.1.0 - 2.1.2", "riskRating": "high", "result": "fail", "magento1": true, "cloudflareCacheHit": null}}{"security.supee6788": {"magento2": false, "resultString": "installed", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.supee8788": {"magento2": false, "resultString": "unknown", "riskRating": "low", "result": "unkn", "magento1": true, "cloudflareCacheHit": null}}{"security.supee9652": {"magento2": false, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.supee10570": {"magento2": false, "resultString": "patched", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.supee10752": {"magento2": false, "resultString": "patched", "riskRating": "low", "result": "ok", "magento1": true, "cloudflareCacheHit": null}}{"security.supee6285": {"magento2": false, "resultString": "unknown", "riskRating": "low", "result": "unkn", "magento1": true, "cloudflareCacheHit": null}}{"security.supee6482": {"magento2": false, "resultString": "unknown", "riskRating": "low", "result": "unkn", "magento1": true, "cloudflareCacheHit": null}}{"security.exposedapi": {"magento2": true, "resultString": "safe", "riskRating": "low", "result": "ok", "magento1": false, "cloudflareCacheHit": null}}{"security.supee10266": {"magento2": false, "resultString": "backend url required", "riskRating": "low", "result": "unkn", "magento1": true, "cloudflareCacheHit": null}}{"security.supee9767": {"magento2": false, "resultString": "unknown", "riskRating": "low", "result": "unkn", "magento1": true, "cloudflareCacheHit": null}}{"security.supee10415": {"magento2": false, "resultString": "unknown", "riskRating": "low", "result": "unkn", "magento1": true, "cloudflareCacheHit": null}}{"security.supee7405": {"magento2": false, "resultString": "unknown", "riskRating": "low", "result": "unkn", "magento1": true, "cloudflareCacheHit": null}}
答案 0 :(得分:4)
https://www.npmjs.com/package/really-relaxed-json 可以在这里帮助您。不幸的是,由于某些原因,StackOverflow片段作为外部资源(来自CDN https://cdn.jsdelivr.net/npm/relaxed-json@1.0.1/relaxed-json.js)作为外部资源包含时无法运行,因此我在此处创建了Stackblitz演示:
https://js-ei2iz2.stackblitz.io <-检查控制台
这是可编辑的示例:
let response = `{
"canonical_url":"http://magento2-demo.nexcess.net/"
}{
"shop_properties":{
"is_hypernode":false,
"hypernode_without_magento":false,
"loadtime_milliseconds":535
}
}{
"admin_url":null
}{
"security.supee5344":{
"magento2":false,
"resultString":"backend url required",
"riskRating":"low",
"result":"unkn",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee5994":{
"magento2":false,
"resultString":"installed",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.cart2quoterce":{
"magento2":false,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.ransomware":{
"magento2":true,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.ajaxcartrce":{
"magento2":false,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.openversioncontrol":{
"magento2":true,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.opendev":{
"magento2":true,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.guruincinfection":{
"magento2":true,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.creditcardhijack":{
"magento2":true,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.visbot":{
"magento2":false,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.cryptojacking":{
"magento2":true,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.outdatedserverversion":{
"magento2":true,
"resultString":"unknown",
"riskRating":"low",
"result":"unkn",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.cacheleak":{
"magento2":true,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.amastyopendownload":{
"magento2":false,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.sslcheck":{
"magento2":true,
"resultString":"misconfigured",
"riskRating":"medium",
"result":"fail",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.webforms":{
"magento2":false,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.openmagmi":{
"magento2":false,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.magversion":{
"magento2":true,
"resultString":"CE 2.1.0 - 2.1.2",
"riskRating":"high",
"result":"fail",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee6788":{
"magento2":false,
"resultString":"installed",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee8788":{
"magento2":false,
"resultString":"unknown",
"riskRating":"low",
"result":"unkn",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee9652":{
"magento2":false,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee10570":{
"magento2":false,
"resultString":"patched",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee10752":{
"magento2":false,
"resultString":"patched",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee6285":{
"magento2":false,
"resultString":"unknown",
"riskRating":"low",
"result":"unkn",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee6482":{
"magento2":false,
"resultString":"unknown",
"riskRating":"low",
"result":"unkn",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.exposedapi":{
"magento2":true,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":false,
"cloudflareCacheHit":null
}
}{
"security.bruteforce":{
"magento2":false,
"resultString":"protected",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee10266":{
"magento2":false,
"resultString":"backend url required",
"riskRating":"low",
"result":"unkn",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee9767":{
"magento2":false,
"resultString":"unknown",
"riskRating":"low",
"result":"unkn",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee10415":{
"magento2":false,
"resultString":"unknown",
"riskRating":"low",
"result":"unkn",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee7405":{
"magento2":false,
"resultString":"unknown",
"riskRating":"low",
"result":"unkn",
"magento1":true,
"cloudflareCacheHit":nulla
}
}`
response = "["+response+"]"
console.log(RJSON.parse(response))
答案 1 :(得分:0)
function parse(str) {
{
let splitted = str.split('}{')
let json = [];
splitted.forEach((s, i) => {
let fullObjectString = i == 0 ? s + '}' : i == splitted.length - 1 ? '{' + s : '{' + s + '}'
json.push(
JSON.parse(fullObjectString)
)
})
return json
}
答案 2 :(得分:0)
我以这种方式将json转换为对象数组,这将解析为对象数组
var x=`{
"canonical_url":"http://magento2-demo.nexcess.net/"
}{
"shop_properties":{
"is_hypernode":false,
"hypernode_without_magento":false,
"loadtime_milliseconds":535
}
}{
"admin_url":null
}{
"security.supee5344":{
"magento2":false,
"resultString":"backend url required",
"riskRating":"low",
"result":"unkn",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee5994":{
"magento2":false,
"resultString":"installed",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.cart2quoterce":{
"magento2":false,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.ransomware":{
"magento2":true,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.ajaxcartrce":{
"magento2":false,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.openversioncontrol":{
"magento2":true,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.opendev":{
"magento2":true,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.guruincinfection":{
"magento2":true,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.creditcardhijack":{
"magento2":true,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.visbot":{
"magento2":false,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.cryptojacking":{
"magento2":true,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.outdatedserverversion":{
"magento2":true,
"resultString":"unknown",
"riskRating":"low",
"result":"unkn",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.cacheleak":{
"magento2":true,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.amastyopendownload":{
"magento2":false,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.sslcheck":{
"magento2":true,
"resultString":"misconfigured",
"riskRating":"medium",
"result":"fail",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.webforms":{
"magento2":false,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.openmagmi":{
"magento2":false,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.magversion":{
"magento2":true,
"resultString":"CE 2.1.0 - 2.1.2",
"riskRating":"high",
"result":"fail",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee6788":{
"magento2":false,
"resultString":"installed",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee8788":{
"magento2":false,
"resultString":"unknown",
"riskRating":"low",
"result":"unkn",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee9652":{
"magento2":false,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee10570":{
"magento2":false,
"resultString":"patched",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee10752":{
"magento2":false,
"resultString":"patched",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee6285":{
"magento2":false,
"resultString":"unknown",
"riskRating":"low",
"result":"unkn",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee6482":{
"magento2":false,
"resultString":"unknown",
"riskRating":"low",
"result":"unkn",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.exposedapi":{
"magento2":true,
"resultString":"safe",
"riskRating":"low",
"result":"ok",
"magento1":false,
"cloudflareCacheHit":null
}
}{
"security.bruteforce":{
"magento2":false,
"resultString":"protected",
"riskRating":"low",
"result":"ok",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee10266":{
"magento2":false,
"resultString":"backend url required",
"riskRating":"low",
"result":"unkn",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee9767":{
"magento2":false,
"resultString":"unknown",
"riskRating":"low",
"result":"unkn",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee10415":{
"magento2":false,
"resultString":"unknown",
"riskRating":"low",
"result":"unkn",
"magento1":true,
"cloudflareCacheHit":null
}
}{
"security.supee7405":{
"magento2":false,
"resultString":"unknown",
"riskRating":"low",
"result":"unkn",
"magento1":true,
"cloudflareCacheHit":null
}
}`;
anotherString = x.replace(/}{/g, '}\,{');
anotherString = '{ "arrayOfObjects":['+anotherString+ ']}';
console.log(anotherString);
答案 3 :(得分:0)
由于您知道,无论如何这都是格式错误的JSON,因此进行字符串操作可能是可以接受的。
例如,将“ \ n} {\ n”替换为“ \ n},{\ n”(尝试使用尽可能长的模式以减少误报的风险,例如在某些字符串中使用} {-不应是JSON字符串中的非编码换行符。)
答案 4 :(得分:0)
如果您使用Python,则可以尝试使用这半个json修复程序
from half_json.core import JSONFixer
s = '''{}{}{}''' # your half json
f = JSONFixer(100)
result = f.fix('['+ s.strip() + ']')
# result.success == True
datas = json.loads(result.line)
# [{},{},{}]