如何获取一个json变量,其值包含" - "

时间:2018-02-14 12:09:04

标签: angularjs angularjs-ng-repeat angularjs-ng-click rectangles

我有一个包含以下内容的json对象:

                  {
                    "-database": "URL",
                    "-id": "Veveve",
                    "value": [
                        "*.serse.pl/*",
                        "*.qq.serser/*",

                        ]
                    }

我的问题是: 接受克林特的json并将我的json对象保存在myjson Virabel中 我如何在ba-panel-title =""设置" -id" 值?

**当我myjson.-id时,我得到了一个未定义的参数

谢谢

2 个答案:

答案 0 :(得分:1)

使用 myjson["-id"]

console.log(myjson["-id"]);

样本

var myjson = {
  "-database": "URL",
  "-id": "Veveve",
  "value": [
    ".serse.pl/",
    ".qq.serser/"
  ]
};

console.log(myjson["-id"]);

答案 1 :(得分:0)

您可以从ng-repeat的对象中提取。这是一个简单的演示:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.myjson = {
    "-database": "URL",
    "-id": "Veveve",
    "value": [
      ".serse.pl/",
      ".qq.serser/"
    ]
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
  <div ng-repeat="(key,val) in myjson">
    <div>KEY: {{key}} | VALUE: {{val}}</div>
  </div>
</div>

如果你有一个对象数组

,这是一个例子

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.myjson = [{
    "-database": "URL",
    "-id": "Veveve",
    "value": [
      ".serse.pl/",
      ".qq.serser/"
    ]
  }, {
    "-database": "URL2",
    "-id": "Wiwiwi",
    "value": [
      ".qq.serser/"
    ]
  }, {
    "-database": "URL3",
    "-id": "Uauaua",
    "value": [
      ".serse.pl/"
    ]
  }];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
  <div ng-repeat="i in myjson">
    <div ng-repeat="(key,val) in i">
      <div>KEY: {{key}} | VALUE: {{val}}</div>
    </div>
    <br>
  </div>
</div>