所以我正在尝试解析一个json文件。我正在使用警报来获取特定字段的值,但每当我传递此特定字符串时,它都显示为nan
myjsondata=JSON.parse(json);
alert(myjsondata.result.parameters.College-name);
Json文件
`{
"id": "1",
"timestamp": "2017-05-11T04:03:26.008Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "hi",
"action": "input.welcome",
"actionIncomplete": false,
"parameters": {
"College-name": "Apex Technical School"},
"contexts": [],
"metadata": {
"intentId": "b11a9493-7c2f-47c0-9928-5653a10c86e9",
"webhookUsed": "false",
"webhookForSlotFillingUsed": "false",
"intentName": "Default Welcome Intent"
},
"fulfillment": {
"speech": "Hi welcome from webfocus Api Ai",
"messages": [
{
"type": 0,
"speech": "Hi welcome from webfocus Api Ai"
},
{
"type": 0,
"speech": ""
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success"
},
"sessionId": "04737548-a3ff-485d-af1a-304edfee9486"
}`
警告行动和其他领域工作正常。但是对于大学来说,它显示为NAN
答案 0 :(得分:2)
您收到空值,因为parameters
没有名为College-name
的密钥:
"parameters": {},
此外,您可能希望使用以下方式更改您的选择:
alert(myjsondata.result.parameters['College-name']);
答案 1 :(得分:0)
您没有关注json style guide,您无法将{json中的任何媒体资源称为.College-name
var myjsondata = {
"id": "1",
"timestamp": "2017-05-11T04:03:26.008Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "hi",
"action": "input.welcome",
"actionIncomplete": false,
"parameters": {
// 'College-name':'aa'
},
"contexts": [],
"metadata": {
"intentId": "b11a9493-7c2f-47c0-9928-5653a10c86e9",
"webhookUsed": "false",
"webhookForSlotFillingUsed": "false",
"intentName": "Default Welcome Intent"
},
"fulfillment": {
"speech": "Hi welcome from webfocus Api Ai",
"messages": [
{
"type": 0,
"speech": "Hi welcome from webfocus Api Ai"
},
{
"type": 0,
"speech": ""
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success"
},
"sessionId": "04737548-a3ff-485d-af1a-304edfee9486"
}
alert(myjsondata.result.parameters['College-name']);
尝试这样,因此它会显示undefined
,因为myjsondata
对象中不存在