简体   繁体   中英

Passing a variable into a JSON-value-request in Javascript

I consider myself to be at somewhat of an intermediate level in Javasript, but I am hitting a major roadblock in trying to access the key (in the key/value pair) of a JSON message that is returned back.

I am writing my code in such a way that the exact key is not static, so it needs to be passed into the JSON-return-message query. For example:

After running the following JSON query:

    var wizards = JSON.parse([some url]);

I get the following data returned back (I formatted it myself to look readable):

    {
    "Status":"Success",
    "IsValidSession":"False",
    "ErrorMessage":"Success",
    "CUSTOM_MARKER_ID_FROM_DCVIEW1":
    [
        {
            "PlaceID":"CUSTOM_MARKER_ID_FROM_DCVIEW",
            "IsVisible":"true",
            "Message":"An e-stop has been pulled.",
            "ImageName":"alert.png",
            "IsPulse":"No"
        }
    ]
    }

"CUSTOM_MARKER_ID_FROM_DCVIEW1" is what needs to be variable based when I query "wizards". I cannot simply just hard-code it in:

    $.each(wizards.CUSTOM_MARKER_ID_FROM_DCVIEW1, function(j, jValue) {/*do some stuff*/});

Is there a way to pass in a variable after "wizards."??

Any help would be much appreciated. Thanks!

Use the square-bracket notation:

var key = "CUSTOM_MARKER_ID_FROM_DCVIEW1";
$.each(wizards[key], function() {  ...  } );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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