I'm going over this now.
At the other end, I'm saving the contents of the datagrid to a txt file, using:
var saveString:String = JSON.stringify(data_grid.dataProvider.toArray());
The data it's saving is like this (complete with square brackets):
[{"Delete":"X","Genre":"none","Duet":""}]
Back to your post, when I load the txt file data back in, this is what I use:
_myData = JSON.parse(event.target.data);// add string data from file to array as objects
_songListDP = new DataProvider(_myData);//add array to dataprovider
data_grid.dataProvider = _songListDP;
From memory, I made _myData an array, as an object was screwing things up, but maybe I'm storing the data in the txt file incorrectly in the first place (causing the object screw up)?
Does my process look incorrect somewhere? Should the square brackets be in the txt file data?
Thanks Sinious
Just open a new AS3 doc (FP11+) and make a string with the data your JSON.stringify() is making and test a JSON.parse() on it directly to an Array:
var str:String = '[{"Delete":"X","Genre":"none","Duet":""}]'; var obj:Array = JSON.parse(str); trace(obj); |
You get the error:
| Scene 1, Layer 'Layer 1', Frame 1, Line 2 | 1118: Implicit coercion of a value with static type Object to a possibly unrelated type Array. |
Because JSON.parse() returns an "Object" and you're assigning it to an "Array".
Even though you typed _myData as an Array, and your JSON is technically an array notation of JSON, the JSON.parse() function does not support returning it as an Array, thus the error above.
Luckily the JSON.parse() will return the object in a notation you can use like an array. Consider the changed source here:
var str:String = '[{"Delete":"X","Genre":"none","Duet":""}]'; var obj:Object = JSON.parse(str); trace(obj[0].Delete + ", " + obj[0].Genre + ", " + obj[0].Duet); |
Trace:
Bottom line, no need to make the array (it's not even valid), just assign the dataProvider at the same time you parse it.
data_grid.dataProvider = new DataProvider(JSON.parse(event.target.data)); |