繁体   English   中英

如何从json嵌套结构中获取键的json值?

[英]How to get json values for keys from json nested structure?

我试图从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"
}]

我怎么能像上面的结构(意味着我只需要单个值,不需要结构中各个键的第二个值/多个值)? 请帮助我,在此先感谢。

使用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); 

您也可以将功能性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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM