0

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!

1 Answer 1

2

Use the square-bracket notation:

var key = "CUSTOM_MARKER_ID_FROM_DCVIEW1";
$.each(wizards[key], function() {  ...  } );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.