简体   繁体   English

我可以在不使用 JSON 库的情况下将字符串写入 .JSON 文件吗

[英]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.我需要在不使用 JSON 库的情况下将格式化的字符串写入 .json 文件。 If I try to use the following code to write a test string I don't get a result inside the .json-File.如果我尝试使用以下代码编写测试字符串,我不会在 .json-File 中得到结果。

FileWriter file = new FileWriter("C:\\Test.json");` 
file.write("test");
  1. Json looks something like that: Json 看起来像这样:
 { "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.然后使用 Brackets 定义结构。

  1. You need to use something like file.flush() when you want to write.当你想写的时候,你需要使用类似 file.flush() 的东西。

Explanation : When you are writing to a file, Java does not want to send each letter to it individually.说明:当您写入文件时,Java 不想将每个字母单独发送给它。 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.为了强制它,发送数据使用flush()-Method。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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