Java object to JSON with org.json lib

前端 未结 2 1302
半阙折子戏
半阙折子戏 2021-02-20 17:16

I have class like this:

public class Class1 {
    private String result;
    private String ip;
    private ArrayList alarm;
}

Wh

2条回答
  •  南笙
    南笙 (楼主)
    2021-02-20 17:56

    I think the utilizing org.json.lib's JSONObject(Object) constructor is what you're looking for. It will construct a JSONObject from your Java Object based on its getters. You can then use JSONObject#toString to get the actual Json produced.

    JSONObject jsonObject = new JSONObject(instanceOfClass1);
    String myJson = jsonObject.toString();
    

提交回复
热议问题