如何使用Java在现有的Json文件上追加数据? [英] How to append data on a existing Json file using Java?

查看:1857
本文介绍了如何使用Java在现有的Json文件上追加数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前在Json文件调用 student.json

This is my current Json data in a Json file call student.json

{"name":"Mahesh","age":10}

我要附加以下数据,输出应如下所示

I want to Append to the following data, output should be like this

{"name":"Mahesh","age":10}, {"name":"BROCK","age":3}, {"name":"Joe","age":4}

这是我当前的代码

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;  

public class JsonWritingProject
{

    public static void main(String[] args)
    {
        JSONObject main = new JSONObject();

        JsonParser jsonParser = new JsonParser();

        try
        {
            Object obj = jsonParser.parse(new FileReader("student.json"));

            JSONObject jsonObject = (JSONObject) obj;

            jsonObject.put("BROCK", 3);
            jsonObject.put("Joe", 4);
            System.out.println(jsonObject.toString());
        } catch (JsonIOException | JsonSyntaxException | FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



       } 

它不起作用,我该怎么办?请指教

It is not working, what should I do? Please advise

推荐答案

更新

哦,我似乎忘记了发布原始的json文件.请在student.json中添加以下内容:(需要包装[]使其成为数组)

Update

Oh seems I forgot to post the original json file. Pls put the student.json with below content: (Need to wrap with [ & ] to make it an array)

[{"name":"Mahesh","age":10}]

因为不能仅将对象追加到原始对象,否则将违反JSON语法,因此将其正确放置的方法是将它们放入数组中.

Because you can not just append objects to original object, it will violate JSON syntax, the way to make it correct is to put them into an array.

您应该更改为使用数组存储多个对象.并使用FileWriter进行输出.

You should change to use an array to store multiple objects. And use FileWriter to output.

请尝试以下代码,它可以在我的本地环境中使用:

Pls try this code, it works in my locl env:

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.*;

public class JsonWritingProject {

    public static void main(String[] args) {
        JSONParser jsonParser = new JSONParser();

        try {
            Object obj = jsonParser.parse(new FileReader("D:\\student.json"));
            JSONArray jsonArray = (JSONArray)obj;

            System.out.println(jsonArray);

            JSONObject student1 = new JSONObject();
            student1.put("name", "BROCK");
            student1.put("age", new Integer(3));

            JSONObject student2 = new JSONObject();
            student2.put("name", "Joe");
            student2.put("age", new Integer(4));

            jsonArray.add(student1);
            jsonArray.add(student2);

            System.out.println(jsonArray);

            FileWriter file = new FileWriter("D:\\student.json");
            file.write(jsonArray.toJSONString());
            file.flush();
            file.close();

        } catch (ParseException | IOException e) {
            e.printStackTrace();
        }

    }
}

与库的依赖关系是:

dependencies {
    compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'
}

这篇关于如何使用Java在现有的Json文件上追加数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆