2016-04-26 116 views
-1

我想從json結構中獲取特定鍵的特定json值。我曾嘗試以下:如何從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" 
}] 

我怎樣才能像上述結構(意味着我只需要單個值,不需要第二個值/結構中各個鍵的多個值)?請幫助我,並提前致謝。

+0

這不是CoffeeScript的;) – Markus

回答

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

+0

感謝您的回覆,我已經盡力了,但對我來說它給錯誤,如jsonstring.map是不是在我CoffeScript的功能。 op = jsonstring.map((item) - > for item i)= if Array.isArray(item [i])then item [i] [0] else item [i] item ) alert op – Dhana

+0

是的我已經解決了在我的腳本中,因爲我在代碼中缺少一個變量。你的代碼工作正常,謝謝你的幫助! – Dhana

1

你也可以使用與lodash/FP功能的CoffeeScript。

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/