1,java对象转 json字符串
java对象/List/复杂java类转成json
JSON.toJSONString(user);
json字符串转java对象.
json字符串转简单java对象:
JSON.parseObject(jsonStr1, User.class);json字符串转List对象:
JSON.parseArray(jsonStr2, User.class);
public void objectTOJson(){
//简单java类转json字符串
User user = new User("dmego", "123456");
String UserJson = JSON.toJSONString(user);
System.out.println("简单java类转json字符串:"+UserJson);
//List<Object>转json字符串
User user1 = new User("zhangsan", "123123");
User user2 = new User("lisi", "321321");
List<User> users = new ArrayList<User>();
users.add(user1);
users.add(user2);
String ListUserJson = JSON.toJSONString(users);
System.out.println("List<Object>转json字符串:"+ListUserJson);
//复杂java类转json字符串
UserGroup userGroup = new UserGroup("userGroup", users);
String userGroupJson = JSON.toJSONString(userGroup);
System.out.println("复杂java类转json字符串:"+userGroupJson);
}
2.json字符串转java对象
@Test
public void JsonTOObject(){
/* json字符串转简单java对象
* 字符串:{"password":"123456","username":"dmego"}*/
String jsonStr1 = "{'password':'123456','username':'dmego'}";
User user = JSON.parseObject(jsonStr1, User.class);
System.out.println("json字符串转简单java对象:"+user.toString());
/*
* json字符串转List<Object>对象
* 字符串:[{"password":"123123","username":"zhangsan"},{"password":"321321","username":"lisi"}]
*/
String jsonStr2 = "[{'password':'123123','username':'zhangsan'},{'password':'321321','username':'lisi'}]";
List<User> users = JSON.parseArray(jsonStr2, User.class);
System.out.println("json字符串转List<Object>对象:"+users.toString());
/*json字符串转复杂java对象
* 字符串:{"name":"userGroup","users":[{"password":"123123","username":"zhangsan"},{"password":"321321","username":"lisi"}]}
* */
String jsonStr3 = "{'name':'userGroup','users':[{'password':'123123','username':'zhangsan'},{'password':'321321','username':'lisi'}]}";
UserGroup userGroup = JSON.parseObject(jsonStr3, UserGroup.class);
System.out.println("json字符串转复杂java对象:"+userGroup);
}
}
fastjson 解析复杂嵌套json字符串
读取一个文件 并转化为json字符串
<!-- FileUtils的依赖-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
File f = new File("src/main/java/com/dzp/a.json");
String js = "";
try {
js = FileUtils.readFileToString(f, "utf-8");
//System.out.println(js);
} catch (IOException e) {
e.printStackTrace();
}
//先将json 字符串转化为 数组
List<Test> tests = JSON.parseArray(js, Test.class);
System.out.println(tests);
这里是要读取的文件
[
{
"id": "user_list",
"key": "id",
"tableName": "用户列表",
"className": "cn.dmego.domain.User",
"column": [
{
"key": "rowIndex",
"header": "序号",
"width": "50",
"allowSort": "false"
},
{
"key": "id",
"header": "id",
"hidden": "true"
},
{
"key": "name",
"header": "姓名",
"width": "100",
"allowSort": "true"
}
]
},
{
"id": "role_list",
"key": "id",
"tableName": "角色列表",
"className": "cn.dmego.domain.Role",
"column": [
{
"key": "rowIndex",
"header": "序号",
"width": "50",
"allowSort": "false"
},
{
"key": "id",
"header": "id",
"hidden": "true"
},
{
"key": "name",
"header": "名称",
"width": "100",
"allowSort": "true"
}
]
}
]
实体类
@Data
@AllArgsConstructor
@EqualsAndHashCode
public class Test implements Serializable {
private String key;
private String id;
private String tableName;
private String className;
private List<Column> column;
}
@Data
@EqualsAndHashCode
@AllArgsConstructor
public class Column implements Serializable {
private String key;
private String header;
private String width;
private String allowSort;
private String hidden;
}
读取文件里面的json字符串 并写到赋值给实体类
public class test2 {
public static void main(String[] args) {
test2 test2 = new test2();
test2.test();
}
public void test(){
File f = new File("src/main/java/com/dzp/a.json");
String js = "";
try {
js = FileUtils.readFileToString(f, "utf-8");
//System.out.println(js);
} catch (IOException e) {
e.printStackTrace();
}
//先将json 字符串转化为 数组
List<Test> tests = JSON.parseArray(js, Test.class);
//System.out.println(tests);
for (Test test : tests) {
ArrayList<Column> columns = new ArrayList<>();
List<Column> column = test.getColumn();
System.out.println(column);
for (Column column1 : column) {
//将 column1 转化为javabean
columns.add(column1);
}
//System.out.println(columns);
}
}}
Map转成实体对象的方法
public static Object map2Object(Map<String, Object> map, Class<?> clazz) {
if (map == null) {
return null;
}
Object obj = null;
try {
obj = clazz.newInstance();
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
int mod = field.getModifiers();
if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
continue;
}
field.setAccessible(true);
String flag = (String) map.get(field.getName());
if(flag != null){
if(flag.equals("false") || flag.equals("true")){
field.set(obj, Boolean.parseBoolean(flag));
}else{
field.set(obj, map.get(field.getName()));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}