views:

122

answers:

1

Hi,

I am making a ajax call, and the backend is gonig to return a JSON array.

Can someone give me an example of what my json array should look like so I can do the following in javascript?

if(myJSON.ErrorCode == 100)
{        
    alert(myJSON.Response.Message);

    for(var x = 0; x < myJSON.Response.Values.Count; x++)
             alert(myJSON.Response.Values[x];
}
else
{
       if(myJSON.ErrorCode == 250)
       {

           alert(myJSON.ErrorMessage);
       }

}
+6  A: 

First case:

{ ErrorCode: 100, Response: { Message : "some message", Values : [0, 1, 2, ... ] } }

Second case:

{ ErrorCode: 250, ErrorMessage: "error message" }
tvanfosson
Is it correct to use { instead of [ for the values part?
mrblah
I'm not sure how strict JSON is, but all keys that come out of the "stringify" method in Douglas Crockford's "json2.js" JSON library are quoted. For example, the second case would be: { "ErrorCode": 100, "ErrorMessage": "error message" }
Steve Harrison
@Steve -- for a library this would be correct behavior as you don't know if the "key" would have spaces in it. If you are hand-crafting the JSON, you only need special handling where appropriate to force the "key" to recognized as an identifier.
tvanfosson
@homestead - the bracket, instead of brace, signifies that this is an array of values rather than an object with properties.
tvanfosson