Is there any way to validate JSON before decode?
Use try.. catch...
import com.adobe.serialization.json.JSONParseError;try{var jsonArray:Array = JSON.decode(loader.data);}catch ( e:JSONParseError ){ //do something trace(e);}finally{}
This solution uses as3corelib (http://as3corelib.googlecode.com/), if you use JSON.parse() please check the answer from JayC
Actionscript suppports try/catch. According to http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/JSON.html, parse
throws a TypeError
on failure. So it looks like something like
try{ var jsonArray:Array = JSON.parse(loader.data);}catch ( e:TypeError ){ //do something}catch (e: SomethingOtherError) //not being literal here, just showing //you can catch more than one type of exception{}finally{ //finally always works, even if the exception is not caught above}
(edited)
The above code shouldn't need your import com.adobe.serialization.json.JSON;
The JSONParseError Thorsten mentions seems to be from the mixpanel library as3corelib library https://github.com/mikechambers/as3corelib/ which uses the JSONDecoder to deserialize https://github.com/mikechambers/as3corelib/blob/master/src/com/adobe/serialization/json/JSONDecoder.as
HOWEVER, if you want to still use the as3corelib library for some reason, to make Thorsten's code work, all you need (IIRC) would be to import com.adobe.serialization.json.JSONParseError;
.