1
{
    'AddUpdateResult': {
        "Patient": "24919"
    }
}

Here is a part of my response, The problem i am facing is the JSON response i am getting is dynamic and i need to parse it. In another scenario, i get something like this.

   {
        'EditUpdateResult': {
            "Patient": "24919"
        }
    }

When i try something like this, i get "Cannot read property Patient from null" and the reason is its expecting "EditUpdateResult".

json.AddUpdateResult.Patient

How can i access the key's dynamically. Like... json.whatever.Patient, so that it may AddUpdateResult or EditUpdateResult

3
  • 1
    Are you only dealing with two possible responses, or do you need a more general solution that can deal with any response that has a "patient" property? Commented May 18, 2012 at 20:46
  • 1
    That isn't JSON. Strings can't be quoted using ' characters. Commented May 18, 2012 at 20:53
  • 1
    JSON properties are quoted with double quotes, not single. Where is the JSON in your code? Is it a string? did you parse it into a JS object? Questions without code are hard to answer. Commented May 18, 2012 at 20:53

2 Answers 2

2

It is possible to iterate over a JSON object without knowing the attribute names.

for (var key in p) {
    if (p.hasOwnProperty(key)) {
        alert(key + " -> " + p[key]);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Using hasOwnProperty is useful if your object may have been prototyped. That probably isn't the case in this example (based on the supplied data), but good point nonetheless.
1

Do all of the responses only have one 'result'? If so try looping over it like this:

var patient;

for (var i in json) {
    patient = json[i].Patient;
}

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.