使用JavaScript读出JSON

时间:2017-07-16 13:29:02

标签: json

给出了以下JSON-File:

{
   "name": "yiisoft/yii2-app-advanced",
   "description": "Yii 2 Advanced Project Template",
   "keywords": ["yii2", "framework", "advanced", "project template"],
   "homepage": "http://www.yiiframework.com/",
   "type": "project",
   "license": "BSD-3-Clause",
   "support": {
       "issues": "https://github.com/yiisoft/yii2/issues?state=open",
       "forum": "http://www.yiiframework.com/forum/",
       "wiki": "http://www.yiiframework.com/wiki/",
       "irc": "irc://irc.freenode.net/yii",
       "source": "https://github.com/yiisoft/yii2"
   },
   "minimum-stability": "dev",
   "require": {
       "php": ">=5.4.0",
       "yiisoft/yii2": "~2.0.6",
       "yiisoft/yii2-bootstrap": "~2.0.0",
       "yiisoft/yii2-swiftmailer": "~2.0.0"
   },
   "require-dev": {
       "yiisoft/yii2-debug": "~2.0.0",
       "yiisoft/yii2-gii": "~2.0.0",
       "yiisoft/yii2-faker": "~2.0.0",
       "codeception/base": "^2.2.3",
       "codeception/verify": "~0.3.1"
   },
   "config": {
       "process-timeout": 1800
   },
   "repositories": [
       {
           "type": "composer",
           "url": "https://asset-packagist.org"
       }
   ]
}

如何读出属性最小稳定性? 以下代码因丢失错误而失败:qnips_JSON_Loesung.html:112 Uncaught ReferenceError:未定义稳定性

output+="<th class='spalte'>"+daten.minimum-stabilty+"</th></tr>";

顺便说一句,以下构造工作非常好:

output+="<th class='spalte'>"+daten.keywords+"</th></tr>";

4 个答案:

答案 0 :(得分:1)

- 让JavaScript认为它是一个计算。

在创建文件时删除它,或者像地图一样访问它。

答案 1 :(得分:1)

此处 daten.minimum-stabilty 与此表达式混淆(即 daten.minimum - stablety )。它认为在 daten.minimum 稳定性之间减去

要访问包含不允许显示为标识符的破折号或其他字符的密钥,请使用括号表示法

daten["minimum-stabilty"];

您是否尝试过这样访问包含-的json密钥?

var json_with_dash_key = daten["minimum-stabilty"];
output+="<th class='spalte'>"+ json_with_dash_key +"</th></tr>";

答案 2 :(得分:0)

使用 daten [&#34; minimum-stabilty&#34;] 应该可以正常使用

与此问题相关的旧帖子中也提出了其他替代方法:Unable to access JSON property with "-" dash

答案 3 :(得分:0)

以下代码解决了我的问题:

for(let j in daten["require-dev"]){
output+="<label> Version "+daten["require-dev"][j]+"<br></label>";
}