实体类
@lombok.Data
public class JsonRootBean {
private int code;
private String message;
private Data data;
private String trade_no;
}
@lombok.Data
public class Data {
private String types;
private String lng;
private String city;
private long num;
private String isp;
private String area_code;
private String city_code;
private String prov;
private String zip_code;
private String lat;
}
JSON转换
public static void main(String[] args) {
//JSONObject转实体类
String str = "{\"code\":200,\"message\":\"处理成功\",\"data\":{\"types\":\"中国联通\",\"lng\":\"114.305392\",\"city\":\"武汉\",\"num\":1329666,\"isp\":\"联通\",\"area_code\":\"420100\",\"city_code\":\"027\",\"prov\":\"湖北\",\"zip_code\":\"430000\",\"lat\":\"30.593098\"},\"trade_no\":\"47e01674745c44edac63322b376a72d6\"}\n";
JsonRootBean jsonRootBean = JSON.parseObject(str, JsonRootBean.class);
System.out.println(jsonRootBean);
JsonRootBean jsonRootBean1 = JSONObject.toJavaObject(JSONObject.parseObject(str), JsonRootBean.class);
System.out.println(jsonRootBean1);
//实体类转JSONObject
System.out.println(JSONObject.toJSONString(jsonRootBean));
//Json字符串转换成JSONObject对象
System.out.println(JSONObject.parseObject(str));
// JsonRootBean(code=200, message=处理成功, data=Data(types=中国联通, lng=114.305392, city=武汉, num=1329666, isp=联通, area_code=420100, city_code=027, prov=湖北, zip_code=430000, lat=30.593098), trade_no=47e01674745c44edac63322b376a72d6)
// JsonRootBean(code=200, message=处理成功, data=Data(types=中国联通, lng=114.305392, city=武汉, num=1329666, isp=联通, area_code=420100, city_code=027, prov=湖北, zip_code=430000, lat=30.593098), trade_no=47e01674745c44edac63322b376a72d6)
// {"code":200,"data":{"area_code":"420100","city":"武汉","city_code":"027","isp":"联通","lat":"30.593098","lng":"114.305392","num":1329666,"prov":"湖北","types":"中国联通","zip_code":"430000"},"message":"处理成功","trade_no":"47e01674745c44edac63322b376a72d6"}
// {"code":200,"data":{"types":"中国联通","lng":"114.305392","city":"武汉","num":1329666,"isp":"联通","area_code":"420100","city_code":"027","prov":"湖北","zip_code":"430000","lat":"30.593098"},"trade_no":"47e01674745c44edac63322b376a72d6","message":"处理成功"}
}
JSON生成JAVA实体类链接