使用Java修改JSON

时间:2018-04-23 16:42:55

标签: java json

您好我有应用程序正在生成JSON数据。当我打开route / someData时,我可以看到JSON,但是当我将其保存在文件中或在Eclipse中打印时,它会在数据之前添加Object名称:

  

MyObjectSchema [纬度= 45.95472,经度= 13.664836,时区=欧洲,   目前= {时间= 1459936800} ...

如何删除列表前的MyObjectSchema。 这是我的代码:

@Scheduled(fixedDelayString = "${fixedDelaySchema}", initialDelayString = "${initialDelaySchema}")
    public MyObjectSchema createMySchema() throws IOException {
        Map<String, ArrayList<MyObject>> map = new HashMap<String, ArrayList<MyObject>>();
        map.put("data", (ArrayList<MyObject>) list1);
        Map<String, Long> map1 = new HashMap<String, Long>();
        map1.put("time", list1.get(0).getTime());

        MyObjectSchema obj1 = new MyObjectSchema();
        obj1.setLatitude(rf.getLatitude());
        obj1.setLongitude(rf.getLongitude());
        obj1.setTimezone(rf.getTimezone());
        obj1.setCurrently(map1);
        obj1.setHourly(map);
        System.out.println(obj1);
        listSchema.add(obj1);

        if (list1.get(0).getTime() == 1460019600) {
            String jsonString = obj1.toString();
            String path = "test.json";

            ObjectOutputStream outputStream = null;
            try {
                outputStream = new ObjectOutputStream(new FileOutputStream(path));
                System.out.println("Start Writings");
                outputStream.writeObject(jsonString);
                outputStream.flush();
                outputStream.close();
                System.exit(0);
            } catch (Exception e) {
                System.err.println("Error: " + e);
            }
        }
        return obj1;
    }

3 个答案:

答案 0 :(得分:2)

obj1.toString()正在调用您的对象toString()的{​​{1}}方法,该方法现在没有生成JSON有效字符串。您可以修改MyObjectSchema方法,以便通过字符串连接生成正确的JSON,但我不推荐它。而是使用像JacksonGson这样的库轻松地从对象中创建JSON。

答案 1 :(得分:1)

Use Libraries like GSON.
To use Gson, declares the following dependency.

pom.xml
<dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.6.2</version>
</dependency>

// 1. Java object to JSON, and save into a file
gson.toJson(obj1, new FileWriter("D:\\test.json"));

答案 2 :(得分:1)

我过去遇到过类似的问题。 你可以像这样解决它:

"[INFO] Running TestSuite
1524549883598   geckodriver INFO    geckodriver 0.20.0
1524549883615   geckodriver INFO    Listening on 127.0.0.1:14395
1524549883939   mozrunner::runner   INFO    Running command: "/usr/lib/firefox/firefox" "-marionette" "-profile" "/tmp/rust_mozprofile.78vmBc1aqdpF"
1524549883941   geckodriver::marionette TRACE     connection attempt 0/600
ExceptionHandler::GenerateDump cloned child 6290
ExceptionHandler::SendContinueSignalToChild sent continue signal to child
ExceptionHandler::WaitForContinueSignal waiting for continue signal...
1524549884041   geckodriver::marionette TRACE     connection attempt 1/600
1524549884141   webdriver::server   DEBUG   <- 500 Internal Server Error {"value":{"error":"unknown error","message":"Process unexpectedly closed with status signal","stacktrace":""}}
Msg Process unexpectedly closed with status signal
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:26:55.152Z'
System info: host: 'Unknown', ip: 'Unknown', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-96-generic', java.version: '1.8.0_131'
Driver info: driver.version: FirefoxDriver
remote stacktrace: