这是一个JSON字符串,在https://jsonlint.com/的一些lint中进行了测试 并且他们都说没关系,我想你会觉得这是一个多余的问题,我理解。但是我陷入了困境,我对这个问题变得疯狂,无法通过我自己修复它。
所以我用这种方式解析JSON字符串(数据var内容在底部):
data = JSON.parse(data);
我试图像往常一样获取数据:
console.log(data.result); or console.log(data['result']);
但它始终是undefined
。有人能帮助我吗?
这里是var数据的内容:
var data = '{"result":"true","id":"31","content":"{"project":{"title":"New","description":"A new file","template":"cve"},"elements":[{"eleTarget":"page-0","eleType":"TEXT"},{"eleTarget":"page-0","eleType":"TEXT"},{"eleTarget":"page-0","eleType":"TEXT"},{"eleTarget":"page-0","eleType":"TEXT"},{"eleTarget":"page-0","eleType":"TEXT"},{"eleTarget":"page-0","eleType":"TEXT"},{"eleTarget":"page-1","eleType":"TEXT"},{"eleTarget":"page-1","eleType":"EXPERIENCE"},{"eleTarget":"page-2","eleType":"TEXT"},{"eleTarget":"page-2","eleType":"EDUCATION"},{"eleTarget":"page-3","eleType":"TEXT"},{"eleTarget":"page-3","eleType":"TEXT"},{"eleTarget":"page-4","eleType":"TEXT"},{"eleTarget":"page-4","eleType":"TEXT"},{"eleTarget":"page-5","eleType":"TEXT"},{"eleTarget":"page-5","eleType":"TEXT"},{"eleTarget":"page-6","eleType":"TEXT"},{"eleTarget":"page-6","eleType":"CREDENTIALS"}],"pages":[{"titleName":"page-0-title","titleValue":"About","fields":[{"fieName":"page-0-ele-0","fieValue":"Mark"},{"fieName":"page-0-ele-1","fieValue":"Hilton"},{"fieName":"page-0-ele-2","fieValue":"A creative"},{"fieName":"page-0-ele-3","fieValue":"developer"},{"fieName":"page-0-ele-4","fieValue":"Grown in the forest educated by robots."},{"fieName":"page-0-ele-5","fieValue":"Some Icon links"}]},{"titleName":"page-1-title","titleValue":"Experience","fields":[{"fieName":"page-1-ele-6","fieValue":"Experience"},{"fieName":"page-1-ele-0-0","fieValue":"Magic developer"},{"fieName":"page-1-ele-0-1","fieValue":"Wizards Ltd."},{"fieName":"page-1-ele-0-2","fieValue":"Providence"},{"fieName":"page-1-ele-0-3","fieValue":"2011"},{"fieName":"page-1-ele-0-4","fieValue":"Create essential magic texts and script code."}]},{"titleName":"page-2-title","titleValue":"Education","fields":[{"fieName":"page-2-ele-7","fieValue":"Education"},{"fieName":"page-2-ele-0-0","fieValue":"Junior Wizard"},{"fieName":"page-2-ele-0-1","fieValue":"Advanced spell management"},{"fieName":"page-2-ele-0-2","fieValue":"Howards"},{"fieName":"page-2-ele-0-3","fieValue":"2001"},{"fieName":"page-2-ele-0-4","fieValue":"Denver"},{"fieName":"page-2-ele-0-5","fieValue":"Texas"}]},{"titleName":"page-3-title","titleValue":"Skills","fields":[{"fieName":"page-3-ele-8","fieValue":"Skills"},{"fieName":"page-3-ele-9","fieValue":"Mobile-First Responsive DesignCross Browser Testing & Debugging Agile Development & Scrum"}]},{"titleName":"page-4-title","titleValue":"Interests","fields":[{"fieName":"page-4-ele-10","fieValue":"Interests"},{"fieName":"page-4-ele-11","fieValue":"Apart from being a web developer, I enjoy most of my time being outdoors."}]},{"titleName":"page-5-title","titleValue":"Awards","fields":[{"fieName":"page-5-ele-12","fieValue":"Awards"},{"fieName":"page-5-ele-13","fieValue":"No awards yet"}]},{"titleName":"page-6-title","titleValue":"Personal details","fields":[{"fieName":"page-6-ele-14","fieValue":"Personal details"},{"fieName":"page-6-ele-0-0","fieValue":"Mark"},{"fieName":"page-6-ele-0-1","fieValue":"Hilton"},{"fieName":"page-6-ele-0-2","fieValue":"Dakota"},{"fieName":"page-6-ele-0-3","fieValue":" 214 987 548 415"},{"fieName":"page-6-ele-0-4","fieValue":"hilton43@email.com"},{"fieName":"page-6-ele-0-5","fieValue":"04-11-78"},{"fieName":"page-6-ele-0-6","fieValue":"Denver TX"},{"fieName":"page-6-ele-0-7","fieValue":"USA"},{"fieName":"page-6-ele-0-8","fieValue":"32,Elm street, Providence NY"},{"fieName":"page-6-ele-0-9","fieValue":"picture"}]}]}","status":"0","edited":"2017-12-08 23:02:28","views":"0"}';
答案 0 :(得分:1)
适合我:
var data = '{"result":"true","id":"31","content":{"project":{"title":"New","description":"A new file","template":"cve"},"elements":[{"eleTarget":"page-0","eleType":"TEXT"},{"eleTarget":"page-0","eleType":"TEXT"},{"eleTarget":"page-0","eleType":"TEXT"},{"eleTarget":"page-0","eleType":"TEXT"},{"eleTarget":"page-0","eleType":"TEXT"},{"eleTarget":"page-0","eleType":"TEXT"},{"eleTarget":"page-1","eleType":"TEXT"},{"eleTarget":"page-1","eleType":"EXPERIENCE"},{"eleTarget":"page-2","eleType":"TEXT"},{"eleTarget":"page-2","eleType":"EDUCATION"},{"eleTarget":"page-3","eleType":"TEXT"},{"eleTarget":"page-3","eleType":"TEXT"},{"eleTarget":"page-4","eleType":"TEXT"},{"eleTarget":"page-4","eleType":"TEXT"},{"eleTarget":"page-5","eleType":"TEXT"},{"eleTarget":"page-5","eleType":"TEXT"},{"eleTarget":"page-6","eleType":"TEXT"},{"eleTarget":"page-6...","titleValue":"Awards","fields":[{"fieName":"page-5-ele-12","fieValue":"Awards"},{"fieName":"page-5-ele-13","fieValue":"No awards yet"}]},{"titleName":"page-6-title","titleValue":"Personal details","fields":[{"fieName":"page-6-ele-14","fieValue":"Personal details"},{"fieName":"page-6-ele-0-0","fieValue":"Mark"},{"fieName":"page-6-ele-0-1","fieValue":"Hilton"},{"fieName":"page-6-ele-0-2","fieValue":"Dakota"},{"fieName":"page-6-ele-0-3","fieValue":" 214 987 548 415"},{"fieName":"page-6-ele-0-4","fieValue":"hilton43@email.com"},{"fieName":"page-6-ele-0-5","fieValue":"04-11-78"},{"fieName":"page-6-ele-0-6","fieValue":"Denver TX"},{"fieName":"page-6-ele-0-7","fieValue":"USA"},{"fieName":"page-6-ele-0-8","fieValue":"32,Elm street, Providence NY"},{"fieName":"page-6-ele-0-9","fieValue":"picture"}]}]},"status":"0","edited":"2017-12-08 23:02:28","views":"0"}';
data = JSON.parse(data);
console.log(data.result);
答案 1 :(得分:1)
您的JSON不可解析。它有一些错误,例如在"
之后和"content":
之前的额外"status":
。这是更新的JSON:
{
"result": "true",
"id": "31",
"content": {
"project": {
"title": "New",
"description": "A new file",
"template": "cve"
},
"elements": [{
"eleTarget": "page-0",
"eleType": "TEXT"
}, {
"eleTarget": "page-0",
"eleType": "TEXT"
}, {
"eleTarget": "page-0",
"eleType": "TEXT"
}, {
"eleTarget": "page-0",
"eleType": "TEXT"
}, {
"eleTarget": "page-0",
"eleType": "TEXT"
}, {
"eleTarget": "page-0",
"eleType": "TEXT"
}, {
"eleTarget": "page-1",
"eleType": "TEXT"
}, {
"eleTarget": "page-1",
"eleType": "EXPERIENCE"
}, {
"eleTarget": "page-2",
"eleType": "TEXT"
}, {
"eleTarget": "page-2",
"eleType": "EDUCATION"
}, {
"eleTarget": "page-3",
"eleType": "TEXT"
}, {
"eleTarget": "page-3",
"eleType": "TEXT"
}, {
"eleTarget": "page-4",
"eleType": "TEXT"
}, {
"eleTarget": "page-4",
"eleType": "TEXT"
}, {
"eleTarget": "page-5",
"eleType": "TEXT"
}, {
"eleTarget": "page-5",
"eleType": "TEXT"
}, {
"eleTarget": "page-6",
"eleType": "TEXT"
}, {
"eleTarget": "page-6",
"eleType": "CREDENTIALS"
}],
"pages": [{
"titleName": "page-0-title",
"titleValue": "About",
"fields": [{
"fieName": "page-0-ele-0",
"fieValue": "Mark"
}, {
"fieName": "page-0-ele-1",
"fieValue": "Hilton"
}, {
"fieName": "page-0-ele-2",
"fieValue": "A creative"
}, {
"fieName": "page-0-ele-3",
"fieValue": "developer"
}, {
"fieName": "page-0-ele-4",
"fieValue": "Grown in the forest educated by robots."
}, {
"fieName": "page-0-ele-5",
"fieValue": "Some Icon links"
}]
}, {
"titleName": "page-1-title",
"titleValue": "Experience",
"fields": [{
"fieName": "page-1-ele-6",
"fieValue": "Experience"
}, {
"fieName": "page-1-ele-0-0",
"fieValue": "Magic developer"
}, {
"fieName": "page-1-ele-0-1",
"fieValue": "Wizards Ltd."
}, {
"fieName": "page-1-ele-0-2",
"fieValue": "Providence"
}, {
"fieName": "page-1-ele-0-3",
"fieValue": "2011"
}, {
"fieName": "page-1-ele-0-4",
"fieValue": "Create essential magic texts and script code."
}]
}, {
"titleName": "page-2-title",
"titleValue": "Education",
"fields": [{
"fieName": "page-2-ele-7",
"fieValue": "Education"
}, {
"fieName": "page-2-ele-0-0",
"fieValue": "Junior Wizard"
}, {
"fieName": "page-2-ele-0-1",
"fieValue": "Advanced spell management"
}, {
"fieName": "page-2-ele-0-2",
"fieValue": "Howards"
}, {
"fieName": "page-2-ele-0-3",
"fieValue": "2001"
}, {
"fieName": "page-2-ele-0-4",
"fieValue": "Denver"
}, {
"fieName": "page-2-ele-0-5",
"fieValue": "Texas"
}]
}, {
"titleName": "page-3-title",
"titleValue": "Skills",
"fields": [{
"fieName": "page-3-ele-8",
"fieValue": "Skills"
}, {
"fieName": "page-3-ele-9",
"fieValue": "Mobile-First Responsive DesignCross Browser Testing & Debugging Agile Development & Scrum"
}]
}, {
"titleName": "page-4-title",
"titleValue": "Interests",
"fields": [{
"fieName": "page-4-ele-10",
"fieValue": "Interests"
}, {
"fieName": "page-4-ele-11",
"fieValue": "Apart from being a web developer, I enjoy most of my time being outdoors."
}]
}, {
"titleName": "page-5-title",
"titleValue": "Awards",
"fields": [{
"fieName": "page-5-ele-12",
"fieValue": "Awards"
}, {
"fieName": "page-5-ele-13",
"fieValue": "No awards yet"
}]
}, {
"titleName": "page-6-title",
"titleValue": "Personal details",
"fields": [{
"fieName": "page-6-ele-14",
"fieValue": "Personal details"
}, {
"fieName": "page-6-ele-0-0",
"fieValue": "Mark"
}, {
"fieName": "page-6-ele-0-1",
"fieValue": "Hilton"
}, {
"fieName": "page-6-ele-0-2",
"fieValue": "Dakota"
}, {
"fieName": "page-6-ele-0-3",
"fieValue": " 214 987 548 415"
}, {
"fieName": "page-6-ele-0-4",
"fieValue": "hilton43@email.com"
}, {
"fieName": "page-6-ele-0-5",
"fieValue": "04-11-78"
}, {
"fieName": "page-6-ele-0-6",
"fieValue": "Denver TX"
}, {
"fieName": "page-6-ele-0-7",
"fieValue": "USA"
}, {
"fieName": "page-6-ele-0-8",
"fieValue": "32,Elm street, Providence NY"
}, {
"fieName": "page-6-ele-0-9",
"fieValue": "picture"
}]
}]
},
"status": "0",
"edited": "2017-12-08 23:02:28",
"views": "0"
}
或者以字符串JS形式:
var data = '{"result":"true","id":"31","content":{"project":{"title":"New","description":"Anewfile","template":"cve"},"elements":[{"eleTarget":"page-0","eleType":"TEXT"},{"eleTarget":"page-0","eleType":"TEXT"},{"eleTarget":"page-0","eleType":"TEXT"},{"eleTarget":"page-0","eleType":"TEXT"},{"eleTarget":"page-0","eleType":"TEXT"},{"eleTarget":"page-0","eleType":"TEXT"},{"eleTarget":"page-1","eleType":"TEXT"},{"eleTarget":"page-1","eleType":"EXPERIENCE"},{"eleTarget":"page-2","eleType":"TEXT"},{"eleTarget":"page-2","eleType":"EDUCATION"},{"eleTarget":"page-3","eleType":"TEXT"},{"eleTarget":"page-3","eleType":"TEXT"},{"eleTarget":"page-4","eleType":"TEXT"},{"eleTarget":"page-4","eleType":"TEXT"},{"eleTarget":"page-5","eleType":"TEXT"},{"eleTarget":"page-5","eleType":"TEXT"},{"eleTarget":"page-6","eleType":"TEXT"},{"eleTarget":"page-6","eleType":"CREDENTIALS"}],"pages":[{"titleName":"page-0-title","titleValue":"About","fields":[{"fieName":"page-0-ele-0","fieValue":"Mark"},{"fieName":"page-0-ele-1","fieValue":"Hilton"},{"fieName":"page-0-ele-2","fieValue":"Acreative"},{"fieName":"page-0-ele-3","fieValue":"developer"},{"fieName":"page-0-ele-4","fieValue":"Grownintheforesteducatedbyrobots."},{"fieName":"page-0-ele-5","fieValue":"SomeIconlinks"}]},{"titleName":"page-1-title","titleValue":"Experience","fields":[{"fieName":"page-1-ele-6","fieValue":"Experience"},{"fieName":"page-1-ele-0-0","fieValue":"Magicdeveloper"},{"fieName":"page-1-ele-0-1","fieValue":"WizardsLtd."},{"fieName":"page-1-ele-0-2","fieValue":"Providence"},{"fieName":"page-1-ele-0-3","fieValue":"2011"},{"fieName":"page-1-ele-0-4","fieValue":"Createessentialmagictextsandscriptcode."}]},{"titleName":"page-2-title","titleValue":"Education","fields":[{"fieName":"page-2-ele-7","fieValue":"Education"},{"fieName":"page-2-ele-0-0","fieValue":"JuniorWizard"},{"fieName":"page-2-ele-0-1","fieValue":"Advancedspellmanagement"},{"fieName":"page-2-ele-0-2","fieValue":"Howards"},{"fieName":"page-2-ele-0-3","fieValue":"2001"},{"fieName":"page-2-ele-0-4","fieValue":"Denver"},{"fieName":"page-2-ele-0-5","fieValue":"Texas"}]},{"titleName":"page-3-title","titleValue":"Skills","fields":[{"fieName":"page-3-ele-8","fieValue":"Skills"},{"fieName":"page-3-ele-9","fieValue":"Mobile-FirstResponsiveDesignCrossBrowserTesting&DebuggingAgileDevelopment&Scrum"}]},{"titleName":"page-4-title","titleValue":"Interests","fields":[{"fieName":"page-4-ele-10","fieValue":"Interests"},{"fieName":"page-4-ele-11","fieValue":"Apartfrombeingawebdeveloper,Ienjoymostofmytimebeingoutdoors."}]},{"titleName":"page-5-title","titleValue":"Awards","fields":[{"fieName":"page-5-ele-12","fieValue":"Awards"},{"fieName":"page-5-ele-13","fieValue":"Noawardsyet"}]},{"titleName":"page-6-title","titleValue":"Personaldetails","fields":[{"fieName":"page-6-ele-14","fieValue":"Personaldetails"},{"fieName":"page-6-ele-0-0","fieValue":"Mark"},{"fieName":"page-6-ele-0-1","fieValue":"Hilton"},{"fieName":"page-6-ele-0-2","fieValue":"Dakota"},{"fieName":"page-6-ele-0-3","fieValue":"214987548415"},{"fieName":"page-6-ele-0-4","fieValue":"hilton43@email.com"},{"fieName":"page-6-ele-0-5","fieValue":"04-11-78"},{"fieName":"page-6-ele-0-6","fieValue":"DenverTX"},{"fieName":"page-6-ele-0-7","fieValue":"USA"},{"fieName":"page-6-ele-0-8","fieValue":"32,Elmstreet,ProvidenceNY"},{"fieName":"page-6-ele-0-9","fieValue":"picture"}]}]},"status":"0","edited":"2017-12-0823:02:28","views":"0"}';
data = JSON.parse(data);
console.log(data);
console.log(data.result);
答案 2 :(得分:0)
嵌套的JSON对象不带引号。删除content
对象周围的引号。
var data = '{"result":"true","id":"31","content":"{"nested":"object"}","status":"0"}';
JSON.parse(data); // SyntaxError
var data = '{"result":"true","id":"31","content":{"nested":"object"},"status":"0"}';
JSON.parse(data); // success