如何将数据插入我的json文件并保存?
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)
}
/////////////////////////////这个不工作////////////
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();
}
这是我的JSON文件
{
"List": [
{
"Name":"somename01"
},
{
"Name":"somename02"
}
],
"assets": "",
"modes": [ "0", "1", "2" ]
}
现在我想在列表项中插入更多数据...
答案 0 :(得分:0)
File
constructor的path
参数应该是完整的文件路径,而不是像"readText.json"
这样的相对路径。
或者,您不必使用完整文件路径,而是使用resolvePath
到find a file relative to a known directory。
示例:
var file:File = File.documentsDirectory.resolvePath("readText.json");