简体   繁体   中英

Can I write Strings into a .JSON-File without using JSON Library

I need to write formatted Strings into a .json File without using the JSON library. If I try to use the following code to write a test string I don't get a result inside the .json-File.

FileWriter file = new FileWriter("C:\\Test.json");` 
file.write("test");
  1. Json looks something like that:
 { "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"] }, "GlossSee": "markup" } } } } }

You have key-value pairs there. Then you use Brackets to define the structure.

  1. You need to use something like file.flush() when you want to write.

Explanation : When you are writing to a file, Java does not want to send each letter to it individually. Therefore it stores them until an certain point is reached if you dont reach that point it wont send the data to the file. To force it, to send the Data use the flush()-Method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM