【问题标题】:How to get json values for keys from json nested structure?如何从 json 嵌套结构中获取键的 json 值?
【发布时间】:2016-08-19 20:27:08
【问题描述】:

我正在尝试从 json 结构中获取特定键的特定 json 值。我尝试了以下方法:

var jsonstring;
jsonstring = JSON.stringify(myjsonObjectArray);
alert(jsonstring);//giving the below json structure

jsonstring = [{
    "key01": [10, "Key01 Description"],
    "key02": [false, "It's a false value"],
    "key03": [null, "Testing Null"],
    "key04": ["tests", "Another Test Value"],
    "key05": [
        [25, 50], "Some testing values"
    ]
}, {
    "key10": [100, "Key10 Value"],
    "key11": [true, "It's a true value for key11"],
    "key12": [null, "key12 values is Null"],
    "key13": ["Testing", "Another Test Value for key13"],
    "key14": [
        [10, 20], "Some other testing values"
    ],
    "key15": ["Test Name", "Name of the key15"]
}]

但我需要如下结构:

jsonstring = [{
    "key01": 10,
    "key02": false,
    "key03": null,
    "key04": "tests",
    "key05": [25, 50]
}, {
    "key10": 100,
    "key11": true,
    "key12": null,
    "key13": "Testing",
    "key14": [10, 20],
    "key15": "Test Name"
}]

我怎样才能得到上面的结构(意味着我只需要单个值,不需要结构中各个键的第二个值/多个值)?请帮助我并提前致谢。

【问题讨论】:

  • 这不是coffeescript ;)

标签: javascript jquery angularjs json coffeescript


【解决方案1】:

使用Array#map,它创建一个新数组,其结果是对该数组中的每个元素调用提供的函数,并从数组中获取第一项,使用for..in循环和从数组中提取first-index-item

var jsonstring = [{
  "key01": [10, "Key01 Description"],
  "key02": [false, "It's a false value"],
  "key03": [null, "Testing Null"],
  "key04": ["tests", "Another Test Value"],
  "key05": [
    [25, 50], "Some testing values"
  ]
}, {
  "key10": [100, "Key10 Value"],
  "key11": [true, "It's a true value for key11"],
  "key12": [null, "key12 values is Null"],
  "key13": ["Testing", "Another Test Value for key13"],
  "key14": [
    [10, 20], "Some other testing values"
  ],
  "key15": ["Test Name", "Name of the key15"],
  "key16": null, //Test Data
  "key17": 'Fake Name' //Test Data
}];
var op = jsonstring.map(function(item) {
  for (var i in item) {
    item[i] = Array.isArray(item[i]) ? item[i][0] : item[i]; //If `item[i]` is not an array
  }
  return item;
});
console.log(op);

【讨论】:

  • 感谢您的回复,我已经尝试过了,但对我来说,它给出了错误,例如 jsonstring.map is not a function in my CoffeScript。 op = jsonstring.map((item) -> for i of item item[i] = if Array.isArray(item[i]) then item[i][0] else item[i] item ) alert op
  • 是的,我已经在我的脚本中解决了这个问题,因为我的代码中缺少一个变量。您的代码运行良好,感谢您的帮助!
【解决方案2】:

您还可以将函数式 CoffeeScript 与 lodash/fp 一起使用。

HTML:

<script src='path/to/lodash.js'></script>
<script src='path/to/lodash.fp.js'></script>

然后:

json = [
        key01: [10, 'Key01 Description']
        key02: [false, 'It\'s a false value']
        key03: [null, 'Testing Null']
        key04: ['tests', 'Another Test Value']
        key05: [
            [25, 50]
            'Some testing values'
        ]
    ,
        key10: [100, 'Key10 Value']
        key11: [true, 'It\'s a true value for key11']
        key12: [null, 'key12 values is Null']
        key13: ['Testing', 'Another Test Value for key13']
        key14: [
            [10, 20]
            'Some other testing values'
        ]
        key15: ['Test Name', 'Name of the key15']
]

convert = _.map _.mapValues _.first

alert JSON.stringify convert json

Lodash 为您提供解决此类问题的辅助函数。 fp 变体允许您组合它们,主要是通过更改参数顺序。 该转换行的意思是“通过仅返回值的第一个元素来映射包含对象的所有值来映射数组中的所有值”,这正是您想要的。

尝试自己: https://jsfiddle.net/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多