lsmpascal
lsmpascal

Reputation: 780

AS3 and JSON : error 1061

I spent hours around a possibly stupid problem relative to JSON encoding in FLASH.

I Try to do something very simple : I make an object and I want it to be serialized in JSON.

But Flash tells me

> 1061: Call to a possibly undefined method encode through a reference with static type Class.

Here is my code. It's a copy of the tutorial here. Something is wrong in it (not in the tutorial I guess, in my code) but I can't figure out what is can be :

import com.adobe.serialization.json.JSON;

var member1:Object = new Object();
member1.firstName = "John"
member1.lastName = "Parker"
member1.age = "32"
member1.country = "Canada"
member1.job = "Programmer"

var member2:Object = new Object();
member2.firstName = "Peter"
member2.lastName = "Anderson"
member2.age = "30"
member2.country = "USA"
member2.job = "System administrator"

var myData:Object = {staff:[member1, member2]};

var myJson:String = JSON.encode(myData);

trace(myJson);

At the line var myJson:String = JSON.encode(myData); flash generate the error mentioned above. Let me precise that the class JSON is well where it has to be (my_project/com/adobe/serialization/json/JSON.as ) and that I can find the encode static method in it.

Can someone tell me where my mistake(s) is or are ? Thank you.

Upvotes: 2

Views: 4026

Answers (2)

lsmpascal
lsmpascal

Reputation: 780

All right. As usual, I found the answer a few seconds after posting my question.

JSON in now incorporated directly in As3, so no need to use com.adobe.serialization.json.JSON

So I delete the import and replaced :

var myJson:String = JSON.encode(myData);

by

var myJson:String = JSON.stringify(myData);

and it works.

Upvotes: 0

Barış Uşaklı
Barış Uşaklı

Reputation: 13532

JSON.parse and JSON.stringfy are the methods you are looking for in Flash Player 11+.

JSON.encode and JSON.decode were part of the Adobe AS3 lib but since the Flash Player has a native JSON parser now you don't need that class.

Upvotes: 6

Related Questions