-
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?
-
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?
-
.
Hi,
That was a quick swap from AS2 to AS3, have a look here http://stackoverflow.com/questions/1...n-actionscript
-
-
.
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.COMPLETE, onload);
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.
-
IT WORKS!!! I have not enough words to say THANK YOU! You are the best!
*close to tears..
-
Designer, Programmer, Musician
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
-
.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|