簡體   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