无法迭代多个 JSON 值

Cannot iterate multiple JSON value

我正在尝试使用 JSON 值在 ng-repeat 中查看数据。但我只得到第一个值。我尝试使用其他方法但没有正确获取值。

我的JSON回复:-

{
  "stylesheet": {
    "attribute-set": [
      {
        "attribute": {
          "_name": "text-align",
          "__prefix": "xsl",
          "__text": "center"
        },
        "_name": "__frontmatter",
        "__prefix": "xsl"
      },
      {
        "attribute": [
          {
            "_name": "space-before",
            "__prefix": "xsl",
            "__text": "80mm"
          },
          {
            "_name": "space-before.conditionality",
            "__prefix": "xsl",
            "__text": "retain"
          },
          {
            "_name": "font-size",
            "__prefix": "xsl",
            "__text": "22pt"
          },
          {
            "_name": "font-weight",
            "__prefix": "xsl",
            "__text": "bold"
          },
          {
            "_name": "line-height",
            "__prefix": "xsl",
            "__text": "140%"
          }
        ],
        "_name": "__frontmatter__title",
        "_use-attribute-sets": "common.title",
        "__prefix": "xsl"
      }
    ],
    "_xmlns:xsl": "http://www.w3.org/1999/XSL/Transform",
    "_xmlns:fo": "http://www.w3.org/1999/XSL/Format",
    "_version": "2.0",
    "__prefix": "xsl"
  }
}

根据上一个问题的建议,我想从 "attribute" 键中获取 "__name" 中的所有数据。

我按照 控制器上的建议进行了尝试:-

console.log($scope.jsonObj);
                angular.forEach($scope.jsonObj,function(value,key){
                 console.log(value["attribute-set"][0]["attribute"]["_name"]);
                    });

jsonObj 是我的 JSON 对象

我的控制台中的输出是 text-align,这是第一个 _name 属性值。

如何从这个 JSON 中实现 _name 值的 ng-repeat?

我假设您需要 _name 的所有值。如果您不这样做,请指定您想要的 _name 值。首先,您必须像这样使用一个数组中的所有属性来重构您的数据:

{
  "stylesheet": {
    "attribute_set": [
      {
        "_name": "text-align",
        "__prefix": "xsl",
        "__text": "center"
      },
      {
        "_name": "__frontmatter",
        "__prefix": "xsl"
      },
      {
        "_name": "space-before",
        "__prefix": "xsl",
        "__text": "80mm"
      },
      {
        "_name": "space-before.conditionality",
        "__prefix": "xsl",
        "__text": "retain"
      },
      {
        "_name": "font-size",
        "__prefix": "xsl",
        "__text": "22pt"
      },
      {
        "_name": "font-weight",
        "__prefix": "xsl",
        "__text": "bold"
      },
      {
        "_name": "line-height",
        "__prefix": "xsl",
        "__text": "140%"
      },
      {
        "_name": "__frontmatter__title",
        "_use-attribute-sets": "common.title",
        "__prefix": "xsl"
      }
    ],
    "_xmlns:xsl": "http://www.w3.org/1999/XSL/Transform",
    "_xmlns:fo": "http://www.w3.org/1999/XSL/Format",
    "_version": "2.0",
    "__prefix": "xsl"
  }
}

然后运行:

angular.forEach($scope.jsonObj.stylesheet.attribute_set, function(value, key) {
    console.log(value._name);
});

数据结构相当糟糕,相同的对象键具有不同的数据类型使事情变得有点困难。但是,这将 return 为您提供所有 _name 字段的列表。

然后您将其绑定到您的范围等。

data
    .stylesheet['attribute-set']
    .map(x => {
        if (Array.isArray(x.attribute))
            return x.attribute.map(y => y['_name']);
        else
            return [x.attribute['_name']]; 
    })
    .reduce((accu, cur) => accu.concat(...cur), []);

它实质上是将每个属性集的 _name 字段提取到一个数组中,然后将其缩减为一个数组。

查看实际效果 here