Using JSON
Been using the actionscript JSON class from JSON.org and found a bug in the implementation. If you're getting exponential numbers from your JSON service, an error is thrown. Something like this will cause you grief:
{
"bigNumber":1.513e8
}Found a way to bypass this, but using such numbers severely affects the stability of Flash 8. I've had Flash 8 disappear instantly after compiling code with such numbers. For some reason, this gets better after you save your file? No idea why, but it's a thing to look out for.
My rewite is really simple. On line 268 (number-function) I've added this:
while (this.next() && (ch>='0' && ch<='9') || ch=='E' || ch=='e' || ch=='-' || ch=='+') {
Comments
Can you show me how you get the JSON string into flash?
load vars, load xml or via xml webservice
I could find a contact e-mail on your website, is this true?
Posted by: Christian | January 10, 2006 02:05 PM
Sure. Just use LoadVars and the Parse-function like this:
If I only had slightly more spare time I'd write a tutorial for Flashmagazine on this...
J
Posted by: Jensa | January 10, 2006 03:03 PM
There is just one problem in your implementation. You will never see a + in strings or numbers, since you use unescape().
For Example
{
"plus":"+",
"bigNumber":1.513e+8
}
will both give false results. "plus" will contain a space after parsing, bignumber will fail because "bigNumber" is unescaped to "1.513e 8".
Posted by: Jens Lehmann | February 27, 2006 03:25 PM
Hi Jens,
That's true. This was the only way around the bug that I could find. By doing this, the class now supports the "e" and "E", but it still does not support the "+" as you say. Feel free to post a solution here if you find one.
J
Posted by: Jensa | February 27, 2006 04:00 PM
instead of the onLoad handler:
jsondata.onData = function(str:String) {
try {
_global.jd = JSON.parse(str);
} catch (ex) {
trace("ERROR parsing JSON string:\n" + ex.name + ": " + ex.message + ": " + ex.at + ": " + ex.text);
}
};
Posted by: Jens Lehmann | March 8, 2006 06:26 PM
Good point Jens! (Better AS2-style catching of errors)
Posted by: Jensa | March 8, 2006 08:31 PM
lemme show you how i've done this.
This combines the onLoad handler that will let u know if you can't load the json AND a parse function that will nicely throw lower level exceptions if the json is malformed.
Dont forget to use
import JSON
in your actionscript, after saving the json class as a separate .as file (and don't forget to CAPITALIZE the file name as in JSON.as).
get the JSON.as contents here:
http://json.org/json.as
Anyway, the c0de2:
import JSON
//jsonStr = "{\"tit\": 12, \"foo\": \"bar\"}"
var jsondata:LoadVars = new LoadVars();
jsondata.onLoad = function(success){
if(success){
parsing( unescape(this.toString()) );
} else {
trace("unable to load JSON data");
}
}
jsondata.load("http://misha.local:8888/video/find_preview_profile/");
function parsing(jsonStr) {
var json = new JSON();
try {
var o:Object = json.parse(jsonStr);
var s:String = json.stringify(obj);
trace(o.fuck);
} catch(ex) {
trace(ex.name + ":" + ex.message + ":" + ex.at + ":" + ex.text);
}
}
Posted by: Michael Rabinovich | April 6, 2007 12:07 AM
Everytime i use this:
import JSON;
var jsondata:LoadVars = new LoadVars();
jsondata.onLoad = function(success){
if(success){
_global.jd = JSON.parse( unescape(this.toString()) );
} else {
trace("unable to load JSON data");
}
}
jsondata.load("http://www.server.com/datasource.php");
i get an error as follows:
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 44: The property being referenced does not have the static attribute.
_global.jd = JSON.parse( unescape(this.toString()) );
I get this with every tutorial i try using JSON. Can you advise?
Posted by: Anonymous | July 18, 2007 11:26 PM
Hi Anonymous,
From the looks of that error message, it seems that you have done some changes to your class that causes this error, so it's not JSON that is the problem. Static classes and variables have to follow certain rules and it looks like you are breaking one of them? I can't tell which from the code you sent.
J
Posted by: Jensa | July 27, 2007 11:35 AM
oh com' on!!!
**
JSON.parse( unescape(this.toString()) );
**
call is assuming that the class JSON has a static function called "parse"
try this:
import JSON;
var jo:JSON = new JSON()
jo.parse(unescape(String));
but then consider the scope where you are calling it...
Posted by: narunas | July 31, 2007 10:29 PM
Ooops. Narunas is right. I should have spotted that. Must not answer comments just before I go to bed...
J
Posted by: Jensa | August 1, 2007 01:52 AM
Just noticed in Michael's code up top that there is a minor mistake:
var o:Object = json.parse(jsonStr);
var s:String = json.stringify(obj);
should be:
var o:Object = json.parse(jsonStr);
var s:String = json.stringify(o);
need to change the "obj" to "o"
Cheers for the code!!
Posted by: JB | June 16, 2008 07:53 AM