javascript - Json解析javascript : String displayed as NAN

标签 javascript json

所以我正在尝试解析一个 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。

最佳答案

您将获得空值,因为 parameters 没有名为 College-name 的键:

"parameters": {},

此外,您可能希望使用以下方法更改您的选择:

alert(myjsondata.result.parameters['College-name']);

关于javascript - Json解析javascript : String displayed as NAN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43906452/

相关文章:

javascript - 如何将文本图编码为json?

java - 在 Spring 中使用 Jackson 来自 JSON 的 Spring 基本 pojo

javascript - 基于选择下拉列表的 Angular 2-Filtering 表(两者都是不同的组件)

javascript - Ember.js URL 如何表示每个可能的路由器状态?

java - 在后台缓存 CSS 和 Javascript 或从应用程序加载它们

json - SpringBoot RestTemplate 忽略 spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false

javascript - Python、Flask - 如何使用 DHTML uploader 加载文件

javascript - 当属性更改时,Angular 服务使用 $watch 进行 $http 调用

c# - Json.Net:将属性序列化/反序列化为一个值,而不是一个对象

ios - 将多个 json 对象存储到文本文件 iOS