Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9260005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:47:24+00:00 2026-06-18T12:47:24+00:00

I have been trying to solve this tough problem but can’t come to a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:47:26+00:00Added an answer on June 18, 2026 at 12:47 pm

    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:

    class FruitColor {
    ...
    public function loadFrom(ob:Object):void {
         this.apple=ob.apple;
         this.banana=ob.banana;
         this.orange=ob.orange;
    }
    ...
    }
    

    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:

    public static function loadFrom(ob:Object):FruitColor {
         var fc:FruitColor=new FruitColor();
         fc.apple=ob.apple;
         fc.banana=ob.banana;
         fc.orange=ob.orange;
         return fc;
    }
    

    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:

    public function makeClassFromObject(ob:Object):String {
        var header:String;
        var footer:String="}\n}";
        var cr:String=""; // constructor header
        var props:String="";
        var crbody:String="";
        for (var k:String in ob) {
            var the_ob=ob[k];
            if (the_ob is Object) {
                header="package { public class "+k+"{\n";
                cr="public function "+k+"(";
                for (k in the_ob) {
                    var v:*=the_ob[k];
                    var tt:String="Object"; // the type
                    if (v is String) tt="String"; else
                    if (v is int) tt="Number"; else 
                    if (v is Number) tt="Number"; else 
                    if (v is Array) tt="Array";
                    props+="private var "+k+": "+tt+";\n";
                    if (cr.charAt(cr.length-1)!='(') cr+=',';
                    cr+=k+":"+tt;
                    crbody+="this."+k+"="+k+";\n";
                }
                cr+="){\n";
                crbody+="}";
            } else {
                throw new Error("A nested Object is required!");
            }
            break; // we need ONE field in ob
        }
        return header+props+cr+crbody+footer;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to solve this problem for a while, but couldn't with
I have been trying to solve this but can't get it to work. http://tinyurl.com/973he2p
I have a tough time trying to solve this problem. I have been at
I have been trying to solve this problem because it is a part of
I've been trying to solve this problem on ACM Timus http://acm.timus.ru/problem.aspx?space=1&num=1932 My first approach
I have been trying to understand and solve this problem for the last three
I have been trying to find ways to solve the problem. Firebug said syntax
I have an interesting problem here I've been trying to solve for the last
I have been trying to solve this for a while now, any help would
I have been trying to solve this for the past couple of days with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.