[Javascript] JSON element has no properties - how to validate?

Michael Borchers list at tridemail.de
Wed Nov 21 02:20:55 CST 2007


I get an httpRequest(myRequest.response) formatted by php into a JSON format, it looks like this:

[{"price":42.4535153,"error":null}]


I handle it this way so far:

var transport            = eval(myRequest.response);
var transportError     = transport[0].error;
var transportPrice    = transport[0].price;

Since "error" is empty (null) I get a warning: "transport has no properties"!

How can I validate if transport[0].error is null without getting a warning?

I tried:
if(transport[0].error == null) { ... }

but it doesn't seem to work.

Do I have to assign "error" differently in the JSON response? f.e. "error":0 ?
Normally "error" would return a string like "No price available".

Thanks


MfG
Michael Borchers
Tridem GmbH
http://www.tridem.de
mailto: borchers at tridem.de
Tel.: 0491 / 96 06 71 63
ICQ: 322766923
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20071121/158ce97a/attachment.htm>


More information about the Javascript mailing list