Vlince
Vlince

Reputation: 6011

How to get values from a Key/Value JSON object

Currently working on an asp.net mvc 4 application.

I have a View that is strongly typed.

Inside the View, I have the following:

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

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

After I console.log(resultJSON), I get the following result:

{
    "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
    }
}

My goal is to have a function in which I can pass a Key such as "RefillRequest":

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

Which in turn, settings would be a dictionary holding only the relevant values based on the Key "RefillRequest" I passed in.

settings would hold something like:

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

I'm in need of a little help with this since I'm no jQuery/Array/Dictionary expert.

Thanks in advance! Sincerely

Vince

Upvotes: 0

Views: 805

Answers (3)

Chokchai
Chokchai

Reputation: 1722

No need a function to do that. To access json value you can just.

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

Upvotes: 1

theftprevention
theftprevention

Reputation: 5213

While Satpal's answer is correct, you can use this to retrieve the dictionary for any key:

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

So, calling mySuperFunction(resultJSON, "RefillRequest") will return resultJSON.RefillRequest, or null if the key isn't in resultJSON.

Upvotes: 1

Satpal
Satpal

Reputation: 133453

Just use resultJSON.RefillRequest, it will fetch you RefillRequest property

As per my understanding, if you need to pass key you can use this

var settings = resultJSON["RefillRequest"];

Upvotes: 2

Related Questions