简体   繁体   English

写JSON object in java前如何去掉花括号

[英]How to remove curly braces before writing JSON object in java

I am trying to make a simple JSON-DB in Java since the current library on maven is horrendously overcomplicated.我正在尝试在 Java 中创建一个简单的 JSON-DB,因为 maven 上的当前库非常复杂。 I have this method that takes in a key and value to put into a JSONObject and write it to my database.json file.我有这种方法,它接受一个键和值以放入 JSONObject 并将其写入我的database.json文件。

public static void put(String path, String key, Object[] value){
        //creates new JSONObject where data will be stored
        JSONObject jsondb = new JSONObject();

        try{
            //adds key and value to JSONObject
            jsondb.put(key, value);

        } catch (JSONException e){
            e.printStackTrace();
        } //end try-catch

        try(PrintWriter out = new PrintWriter(new FileWriter(path, true))) {
            out.write(jsondb.toString());
            out.write(',');
        } catch (Exception e){
            e.printStackTrace();
        } //end try-catch
    } //end put()

Here is my main method where I write some data to the file这是我的主要方法,我将一些数据写入文件

public static void main(String[] args) throws Exception {

        String path = "app.json";

        Object[] amogus = {"Hello", 1, 2, 3, 4, 5};
        Object[] amogus1 = {"Hello", 1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

        JsonDB db = new JsonDB(path);

        db.put(path, "arr", amogus);
        db.put(path, "arr1", amogus1);

    }

What happens is that it save each data in a set of curly braces.发生的事情是它将每个数据保存在一组花括号中。 So when I write to the file more than once like I do in my main method it saves it like this:因此,当我像在 main 方法中那样多次写入文件时,它会像这样保存它:

{"arr": ["Hello", 1, 2, 3, 4, 5]}{"arr1": ["Hello", 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]}

This causes VSCode to throw an error since this isnt valid JSON. How would I make the method remove the curly braces and add commas to make the above JSON valid JSON?这会导致 VSCode 抛出错误,因为这不是有效的 JSON。我如何使该方法删除花括号并添加逗号以使上面的 JSON 有效 JSON? I cant seem to find the documentation for this library (The library is org.json on maven).我似乎找不到这个库的文档(该库是 org.json on maven)。

This is a syntax issue.这是语法问题。 Your json is not valid because the json syntax want your data to be between curly braces like this:您的json无效,因为json语法希望您的数据位于花括号之间,如下所示:

{
    "arr": ["Hello", 1, 2, 3, 4, 5],
    "arr1": ["Hello", 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
}

instead of this, which is not a valid json :而不是这个,它不是有效的json

{"arr": ["Hello", 1, 2, 3, 4, 5]}
{"arr1": ["Hello", 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]}

Create your data in your map object and let your json library convert (serialize) your object in a valid json .在您的 map object 中创建您的数据,并让您的json将您的 object 转换(序列化)为有效的json

I solved this problem by reading all of the content in the file and appending the JSON object that I wanted to write to the file and then write it all at once.我通过读取文件中的所有内容并将我想写入的 JSON object 追加到文件中然后一次全部写入来解决了这个问题。

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

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