实体类和json串的转换

import com.google.gson.Gson;

javaBean转换为json格式:

String str = new Gson().toJson(javaBean);

json字符串转换为bean:

String jsonStr;    //拼接form表单字段值

Gson g = new Gson();

ChqyhzBean hz = g.fromJson(jsonStr, ChqyhzBean.class);

private static String buildUrl() {
String url3 = null;
List<AgreeBean> list = new ArrayList<>();
String title = "";
list.add(new AgreeBean("《xxx》", url1));
list.add(new AgreeBean("《yyy》", url2));
list.add(new AgreeBean("《zzz》", url3));
title = "登录";
String pageTitle = "文字";
Gson gson = new Gson();
String result = gson.toJson(list);

url3 = CC_URL + "?systemSign=YYYYTTTT&sSign=NNNN&agreement=" + result + "&title=" + title + "&pageTitle=" + pageTitle;

return url3;

}


JSONObject使用方法详解

所需jar包

json-lib-2.4-jdk15.jar 
commons-beanutils-1.8.3.jar
commons-collections-3.2.1.jar
commons-lang-2.5.jar
commons-logging-1.1.1.jar
ezmorph-1.0.6.jar

其中 JSONObject 目前最新的版本为2.4,其他版本下载地址为:http://sourceforge.net/projects/json-lib/files/json-lib/


/**
* 通过原生生成json数据格式
*/
JSONObject jsonObject=new JSONObject();
jsonObject.put("name","李四");
jsonObject.put("home",true);
jsonObject.put("number",134.11);
jsonObject.put("sex","男");
System.out.println(jsonObject.toString());

/**通过map来生成json数据格式
*/
HashMap<String,Object> map=new HashMap<String, Object>();
map.put("name","张三");
map.put("home",true);
map.put("技能",new String[]{"json","java","python"});
System.out.println(JSONObject.fromObject(map).toString());
JSONArray mapJson=JSONArray.fromObject(map); //数组格式
mapJson.add(1, new String[]{"dajhdka","dagdhag"});
// mapJson.add(2,"是否有女朋友","");
System.out.println(mapJson.toString());

/**
* 通过实体类生成json数据格式
*/
User user=new User("小红","女",1550.2,false);
JSONObject jsonObject1=JSONObject.fromObject(user);
System.out.println(jsonObject1);

@Test
public void TestGSON()
{
GsonBuilder gsonBuilder=new GsonBuilder(); //设定格式的
User user=new User("小红","女",1550.2,false,new Date());
/**
* String toJson(object) 返回值是String 将对象转换成json格式
*/
gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss"); //设定date属性 有误json本身没有date类型
Gson gson=gsonBuilder.create();
String Gjson=gson.toJson(user);
System.out.println(Gjson);
/**
* 将JSON格式转换成实体类
* 不清楚为什么我用了 复用功能的话 json格式转换成的实体类就没有值了 就是无法定位到
*/
String content="{"name":"小红","sex":"女","number":1550.2,"home":false,"birthday":"2020-02-17"}";
User user1=gson.fromJson(content,User.class);
System.out.println(user1);


}

JSONObject读取外部json文件

JSON.json:
{
"name": "小红",
"sex": "女",
"number": 1667.2,
"home": false

}

@Test
public void JSONTest() throws IOException {
//获取json文档
/**
* class.getResource("") 会获取target底下的class的 上一级的包
* 这里是 file:/D:/IdeaProjects/Web/JSON_GSON__TEST/target/test-classes/test/
*/
// System.out.println(JSONTextParsing.class.getResource(""));
File file=new File(JSONTextParsing.class.getResource("JSON.json").getFile());//找到该文件
/**
* 使用阿帕奇的jar包 commons-io
* 获取文本信息 String类型 readFileToString 并保留格式
*/
String content=FileUtils.readFileToString(file);
JSONObject jsonObject=JSONObject.fromObject(content);
System.out.println(jsonObject);


}



原文地址:https://www.cnblogs.com/polo/p/13793607.html