2013-10-02 80 views
0

當前正在使用asp.net mvc 4應用程序。如何從鍵/值JSON對象中獲取值

我有強烈類型的視圖。

裏面的觀點,我有以下幾點:

<script type="text/javascript"> 
    $(document).ready(function() { 

     var resultJSON = $.parseJSON(JSON.stringify(@Html.Raw(Model.Json))); 
     console.log(resultJSON); 
    }); 
</script> 

後,我console.log(resultJSON),我得到以下結果:

{ 
    "Log": { 
     "ShowFormatDropdown": true, 
     "ShowGroupDropdown": false, 
     "ShowStartDateAndYearDropdown": false, 
     "ShowEndDateAndYearDropdown": false, 
     "ShowStartEndDateTextbox": true, 
     "ShowMachineNameDropdown": true, 
     "ShowSeverityDropdown": true, 
     "ShowSummaryDetailRadioButton": false, 
     "ShowMessageTextbox": true, 
     "ShowIPAddressTextbox": true, 
     "ShowCorrelationTextbox": true, 
     "ShowDINTextbox": false 
    }, 
    "RefillRequest": { 
     "ShowFormatDropdown": true, 
     "ShowGroupDropdown": true, 
     "ShowStartDateAndYearDropdown": true, 
     "ShowEndDateAndYearDropdown": true, 
     "ShowStartEndDateTextbox": false, 
     "ShowMachineNameDropdown": false, 
     "ShowSeverityDropdown": false, 
     "ShowSummaryDetailRadioButton": true, 
     "ShowMessageTextbox": false, 
     "ShowIPAddressTextbox": false, 
     "ShowCorrelationTextbox": false, 
     "ShowDINTextbox": false 
    }, 
    "PatientSubscriptions": { 
     "ShowFormatDropdown": true, 
     "ShowGroupDropdown": true, 
     "ShowStartDateAndYearDropdown": true, 
     "ShowEndDateAndYearDropdown": true, 
     "ShowStartEndDateTextbox": false, 
     "ShowMachineNameDropdown": false, 
     "ShowSeverityDropdown": false, 
     "ShowSummaryDetailRadioButton": true, 
     "ShowMessageTextbox": false, 
     "ShowIPAddressTextbox": false, 
     "ShowCorrelationTextbox": false, 
     "ShowDINTextbox": false 
    } 
} 

我的目標是有一個功能,我可以通過一個Key如「補充請求」:

var settings = mySuperFunction(resultJSON, "RefillRequest"); 

這反過來,settings將是一本字典僅基於「RefillRequest」我傳遞的主要持有相關值

settings將舉行類似:

"ShowFormatDropdown": true, 
"ShowGroupDropdown": true, 
"ShowStartDateAndYearDropdown": true, 
"ShowEndDateAndYearDropdown": true, 
"ShowStartEndDateTextbox": false, 
"ShowMachineNameDropdown": false, 
"ShowSeverityDropdown": false, 
"ShowSummaryDetailRadioButton": true, 
"ShowMessageTextbox": false, 
"ShowIPAddressTextbox": false, 
"ShowCorrelationTextbox": false, 
"ShowDINTextbox": false 

我在需要一點幫助,因爲我不是jQuery/Array/Dictionary專家。

在此先感謝! 真誠

文斯

+0

爲什麼不用點符號或方括號來訪問它? –

+0

你爲什麼要問?誠實地說,儘管聽起來很瘋狂,但我沒有考慮它,並且一直在想它。幸運的是,像你這樣的人在這裏幫助:-) – Vlince

回答

2

只要使用resultJSON.RefillRequest,它會爲你抓取RefillRequest財產

按我的理解,如果你需要傳遞鍵,你可以使用這個

var settings = resultJSON["RefillRequest"]; 
+0

謝謝你...你不知道怎麼過於複雜,我在看這個。 – Vlince

1

無需一函數來做到這一點。要訪問json值,你可以。

var settings = resultJSON.Log; 
//or 
var settings = resultJSON['Log']; 
1

雖然Satpal的答案是正確的,你可以使用這個檢索字典任何鍵:

function mySuperFunction(obj, key) { 
    return (key in object) ? object[key] : null; 
} 

因此,呼籲mySuperFunction(resultJSON, "RefillRequest")將返回resultJSON.RefillRequest,或null如果該鍵不在resultJSON

+0

謝謝,我也喜歡你的解決方案。請繼續關注。 – Vlince