I have been trying to solve this tough problem but can’t come to a good approach.
So I have a JSON string:
{
"fruit_color":{
"apple":"",
"banana":"",
"orange":""
}
}
I want to process this JSON to get an Object out of it which represents the class:
class FruitColor
{
private var apple: String;
private var banana: String;
private var orange: String;
public function FruitColor(apple: String, banana: String, orange: String)
{
this.apple=apple;
this.banana=banana;
this.orange=orange;
}
}
Is this possible?
Would it be better if I use the JSON string and try to create code out of it?
The reason I need to do this is that I have a certain JSON I need to use to create class structures out of in ActionScript, Java, objective C to create libraries in those languages. So I am looking at something where I can specify all details once, so I dont have to individually monitor all the separate library codes.
Well, it is possible, but a JSON parser will not return you an instance of FruitColor, but an Object instead. So, with this JSON string, you will receive an object with a “fruit_color” field of type Object, that in turn will have three fields of type String. Say, this “fruit_color” actually determines the complete FruitColor instance, in this case you can do like this:
And call this, creating a new instance of FruitColor, then applying the parsed object within
loadFrom(). Or, make a similar static function that will return a ready-made FruitColor object like this:Edit: Given that you want a *.as file out of a JSON object template, wanting a constructor that will provide necessary values, you need an approach similar to this: