A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Load and display data from JSON file.

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    32

    Load and display data from JSON file.

    So I have this json file with a name demo.json
    It is basically a notepad file with text:

    {
    "type":"Toy",
    "animal": "Bear",
    "data": {
    "name": "Timmy",
    "color": "Brown",
    "price": "10",
    "madein": "USA"
    }
    }


    And I need this data to be loaded and displayed in flash swf file. Maybe there is someone who knows how it could be done?

  2. #2
    Member
    Join Date
    Mar 2014
    Posts
    32
    UPDATE : Ok, so I make 6 dynamic text fields in AS3 with instance names

    type.txt
    animal.txt
    name.txt
    color.txt
    price.txt
    madein.txt


    and I need the data from JSON file to be displayed into those text fields.
    Coud You please tell me what would be the correct code for that?

  3. #3
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    That was a quick swap from AS2 to AS3, have a look here http://stackoverflow.com/questions/1...n-actionscript

  4. #4
    Member
    Join Date
    Mar 2014
    Posts
    32

    Red face

    Quote Originally Posted by fruitbeard View Post
    Hi,

    That was a quick swap from AS2 to AS3, have a look here http://stackoverflow.com/questions/1...n-actionscript
    Thank You Yes, im working on several projects and I have to get them completed in very short time , so Im switching from one to other AS.

    So thus far I have made changes in the instance names, because I read that using dot in instance name is not right.. So I retyped them as type_txt, animal_txt ..etc

    Also I managed to found an useful code in which URLLoader is used to load the file :

    //import Jason.as class
    import com.adobe.serialization.json.JSON


    var loader:URLLoader = new URLLoader();
    var request:URLRequest = new URLRequest();


    request.url = "demo.json";
    loader.load(request);

    loader.addEventListener(Event.COMPLETE, decodeJSON);


    the .fla file : toy.fla

    About the code - im not sure if the demo.json file is correct? Maybe it should be called just demo.txt?

    also I have found out that in order to work with JSON in Flash this file needs to be downloaded (its a JSON class?) https://github.com/mikechambers/as3corelib and from this file, the folder "com" needs to be placed in the same place where our fla and json file is.

    But from this place, im stucked, and pretty much I have no idea what to do next.

    p.s this is my first day working with JSON and managing to load json files in adobe flash.

  5. #5
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    Start by formatting the JSON properly (or better), like so
    Code:
    {
     "Toys":
     [
      {
       "type":"Toy",
       "animal":"Bear",
       "name":"Timmy",
       "color":"Brown",
       "price":"10",
       "madein":"USA"
      },
      {
       "type":"Toy",
       "animal":"Snake",
       "name":"Toby",
       "color":"Red",
       "price":"12",
       "madein":"CHINA"
      }
     ]
    }
    you will need to embed your font, then use this code
    PHP Code:
    import com.adobe.serialization.json.JSON;

    var 
    myRequest:URLRequest = new URLRequest("demo.json");// or dedmo.txt - up to you
    var myLoader = new URLLoader();
    myLoader.addEventListener(Event.COMPLETEonload);
    myLoader.load(myRequest);

    function 
    onload(e:Event):void
    {
        var 
    myData:Object JSON.parse(myLoader.data);
        
    // *** Trace Toys value length;
        
    trace("Toys: " myData.Toys.length);
        
    // *** Trace first values;
        
    trace("Type: " myData.Toys[0].type);
        
    trace("Animal: " myData.Toys[0].animal);
        
    trace("Name: " myData.Toys[0].name);
        
    trace("Color: " myData.Toys[0].color);
        
    trace("Price: " myData.Toys[0].price);
        
    trace("Made In: " myData.Toys[0].madein);
        
        
    // ** Spit out second values to text;
        
    type_txt.text myData.Toys[1].type;
        
    animal_txt.text myData.Toys[1].animal;
        
    name_txt.text myData.Toys[1].name;
        
    color_txt.text myData.Toys[1].color;
        
    price_txt.text myData.Toys[1].price;
        
    madein_txt.text myData.Toys[1].madein;

    and assuming you placed the JSON Class files in the correct location, it should now work for you.

  6. #6
    Member
    Join Date
    Mar 2014
    Posts
    32
    IT WORKS!!! I have not enough words to say THANK YOU! You are the best!
    *close to tears..

  7. #7
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Related to this, i was reading about JSON file last week, and i wonder if i can write data to the JSON files from AS3. I already write/read data from XML/TXT with PHP, and with the XML/XML Sockets Classes, but i was searching for a non-installable database like SQLITE, but if there's a way of writing data to a JSON file, that would be great. Write and Read to/from JSON files. Thanks for the suggestions
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  8. #8
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    You have the basics of reading a json file in the posts above, so yes it can be read by flash.
    You can also write to a json file using flash & php (after all it is only a txt file really), send the data from flash as you would a txt file and have php do the formatting, or you could even send the formatting from flash too with some slight modificiation.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center