vendredi 19 février 2016

How can I insert data to my json file and save it in local with AS3?

How can I insert data to my json file and save it?

private function readAndWrite() {

        listJson = new URLLoader();
        listJson.load(new URLRequest("readText.json"));
        listJson.addEventListener(Event.COMPLETE, parseListJson);
    }
    private function parseListJson(evt: Event) {

        readJsonList = JSON.parse(listJson.data);
        var numberOfBooks = readJsonList.BookList.length
        readJsonList.BookList[numberOfBooks] = {
            Name: "Hello World"
        }

        for (var i = 0; i <= numberOfBooks; i++) {
            trace(readJsonList.BookList[i].Name)
        }

/////////////////////////////This one not working////////////

        var file: File = new File("readText.json");
        trace(file +" :file name")
        var stream: FileStream = new FileStream();
        stream.open(file, FileMode.WRITE);
        stream.writeUTFBytes(readJsonList.BookList as String);
        stream.close();
    }

And here is my JSON file

 {
   "List": [
    {
      "Name":"somename01"
    },
   {
      "Name":"somename02"
   }
  ],
   "assets": "", 
   "modes":     [  "0", "1", "2" ]
}

Now I want to insert more data to my list items...

Aucun commentaire:

Enregistrer un commentaire