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

Rafael Menezes soscpd at gmail.com
Wed Nov 21 07:05:04 CST 2007


Hi Michael

Can you try if(transport[0].error)?

Regards
Rafael


On 21/11/2007, Michael Borchers <list at tridemail.de> wrote:
>
>  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
>
> _______________________________________________
> Javascript mailing list
> Javascript at lists.evolt.org
> http://lists.evolt.org/mailman/listinfo/javascript
>



-- 
Regards
Rafael Menezes
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20071121/28ccd061/attachment.htm>


More information about the Javascript mailing list