简体   繁体   English

将jsonobject追加到现有的jsonobject

[英]Appending jsonobject to an existing jsonobject

I'm having a problem with Json file reading and writing. 我在读取和写入Json文件时遇到问题。 I want to append something into a json file but it doesn't work properly: it just put in a new jsonobject without the ' , ' to divide it from the previous one. 我想将某些内容附加到json文件中,但无法正常工作:它只是放入了一个新的json对象,而没有使用' '将其与上一个对象分开。 I searched everywhere, on every site, but nothing that gave me an input on how to do it properly. 我在每个站点的每个地方进行了搜索,但是没有任何信息可以使我正确地进行操作。

For example, I have a json file like this: 例如,我有一个像这样的json文件:

{
    "Example":{
        "Ok":"Ok1",
        "Nice":"Nice1",
        "Hi":"Hi1",
        "Hello":"Hello1",
        "Right":"Right1",
        "Wow":"Wow1"
    }
}

And I want to make it appear like this: 我想使它看起来像这样:

{
    "Example":{
        "Ok":"Ok1",
        "Nice":"Nice1",
        "Hi":"Hi1",
        "Hello":"Hello1",
        "Right":"Right1",
        "Wow":"Wow1"
    },
    "Example1":{
        "Ok":"Ok2",
        "Nice":"Nice2",
        "Hi":"Hi2",
        "Hello":"Hello2",
        "Right":"Right2",
        "Wow":"Wow2"
    }
}

So, I tried using this code: 因此,我尝试使用此代码:

Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonObject jsonObject = new JsonObject();
JsonObject dati = new JsonObject();
dati.addProperty("Cognome", StringUtils.capitalize((fields[0].getText())));
dati.addProperty("Nome", StringUtils.capitalize((fields[1].getText())));
dati.addProperty("Sesso", lblSesso.getText());
dati.addProperty("Luogo di nascita", StringUtils.capitalize((fields[2].getText())));
dati.addProperty("Provincia", lblProvincia.getText());
dati.addProperty("Data di nascita", fieldDDN.getText());
jsonObject.add(codfis, dati);
String json = gson.toJson(jsonObject);
try (BufferedReader br = new BufferedReader(new FileReader("CodFisCalcolati.json"));
    BufferedWriter bw = new BufferedWriter(new FileWriter("CodFisCalcolati.json", true))) {
    String jsonString = gson.fromJson(br, JsonElement.class).toString();
    JsonElement jelement = new JsonParser().parse(jsonString);
    JsonObject jobject = jelement.getAsJsonObject();
    jobject.add(codfis, dati);
    String resultingJson = gson.toJson(jelement);
    bw.write(resultingJson);
    bw.close();
} catch (IOException e1) { e1.printStackTrace(); }

But when I use it, it give me this output : 但是当我使用它时,它会给我以下输出:

{
    "Example":{
        "Ok":"Ok1",
        "Nice":"Nice1",
        "Hi":"Hi1",
        "Hello":"Hello1",
        "Right":"Right1",
        "Wow":"Wow1"
    }
}{
    "Example":{
        "Ok":"Ok1",
        "Nice":"Nice1",
        "Hi":"Hi1",
        "Hello":"Hello1",
        "Right":"Right1",
        "Wow":"Wow1"
    },
    "Example1":{
        "Ok":"Ok2",
        "Nice":"Nice2",
        "Hi":"Hi2",
        "Hello":"Hello2",
        "Right":"Right2",
        "Wow":"Wow2"
    }
}

That's output, you see, it'wrong and i don't know how to make the code to give me a different output. 那是输出,您看,这是错误的,我不知道该如何编写代码才能为我提供不同的输出。

I'm using Gson 2.8.5 and I would rather not change to another library. 我正在使用Gson 2.8.5,但我不想更改为其他库。

You change the question but now the answer to your new question is you use the same file to read and write. 您更改了问题,但是现在新问题的答案是使用相同的文件进行读写。 That's why you add the data inside ot the file. 这就是为什么在文件内部添加数据的原因。 Change the name of the file that you write and see if you have problems 更改所写文件的名称,看看是否有问题

Please check if "br" is not null. 请检查“ br”是否不为空。

According to the specification of the method fromJson it returns: an object of type T from the string. 根据fromJson方法的规范,它返回:字符串中类型为T的对象。 Returns null if json is null. 如果json为null,则返回null。

If this is the case than you call on the null toString() method and you get null pointer exception 如果是这种情况,那么您将调用null的toString()方法,并且将获得null指针异常

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

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