java中json字符串与对象转换
常见的转换⼯具有:
Jackson:SpringMVC内置的转换⼯具
jsonlib:Java提供的转换⼯具(⼀般不⽤)
gson:google提供的转换⼯具(轻量级的框架)
fastjson:Alibaba提供的转换⼯具(效率⾼速度快)
Jackson:
相关jar包:
jackson-annotations-2.2.3.jar
jackson-core.2.2.3.jar
jackson-databind-2.2.3.jar
主要⽅法:
//对象----->字符串
String jsonStr = new ObjectMapper().**writeValueAsString**(resultBean);
//字符串--->map
Map map = JSON.**parseObject**(jsonStr, Map.class);
//字符串---> list
TypeReference<List<User>> ref = new TypeReference<List<User>>(){};
List<User> list = adValue(jsonStr, ref);
java对象转json:
//Map对象或者JavaBean对象转换成json的时候会得到⼀个json字符串
//List<Map>或者List<JavaBean>转换成json的时候会得到⼀个json数组的字符串
@Test
public void test04() throws JsonProcessingException {
List<User> userList = new ArrayList<>();
userList.add(new User(1, "娜扎", "123456", "189********@163", "189********"));
userList.add(new User(2, "热巴", "123456", "189********@163", "198********"));
userList.add(new User(3, "哈尼", "123456", "189********@163", "198********"));
ResultBean resultBean = new ResultBean(true, userList);
String jsonStr = new ObjectMapper().writeValueAsString(resultBean);
System.out.println(jsonStr);
}
json字符串转对象:
使⽤jackson将json字符串转换JavaBean对象或者Map
@Test
//把json转成JavaBean(user对象)
public void test06() throws IOException {
String jsonStr = "{\"id\":1,\"username\":\"zs\",\"password\":\"123456\",\"email\":\"zs@163\",\"phone\":\"1386789898\"}";
//1.调⽤JSON.parseObject(String json,Class clazz);
//转换成user
User user = JSON.parseObject(jsonStr, User.class);
System.out.println(user);
//转换成map
Map map = JSON.parseObject(jsonStr, Map.class);
System.out.println(map);
}
使⽤jackson将json数组字符串转换成List
@Test
//把json转成List<JavaBean>对象
public void test07() throws Exception {
String jsonStr = "[{\"id\":1,\"username\":\"zs\",\"password\":\"123456\",\"email\":\"zs@163\",\"phone\":\"1386789898\"},{\"id\":2,\"username\":\"ls\",\"password\":\"123456\",\"email\":\"ls@163\",\"phone\":\"1386781898\"},{\"id\":3,\"usern //1.创建ObjectMapper对象
ObjectMapper objectMapper = new ObjectMapper();
//2.调⽤readValue()
TypeReference<List<User>> ref = new TypeReference<List<User>>(){};
List<User> list = adValue(jsonStr, ref);
System.out.println(list);
}
fastjson
jar包:fastjson-1.2.39.jar
主要⽅法:
String jsonStr = JSONString(user) //user(对象) -----> jsonStr(字符串)
Map map = JSON.parseObject(jsonStr, Map.class); //jsonStr(字符串) --> map
List<User> userList = JSON.parseArray(jsonArr, User.class); //jsonStr(字符串) --> List
使⽤fastjson将java对象转成json字符串
@Test
public void test06(){
//使⽤fastjson将user对象转换成json字符串
User user = new User(1,"张三","123456","123456@qq","189********");
String jsonStr = JSONString(user);
System.out.println(jsonStr);
}
使⽤fastjson将json字符串转换成JavaBean对象或者Map
@Test
//把json转成JavaBean(user对象)
public void test08() throws IOException {
String jsonStr = "{\"id\":1,\"username\":\"zs\",\"password\":\"123456\",\"email\":\"zs@163\",\"phone\":\"1386789898\"}";
//1.调⽤JSON.parseObject(String json,Class clazz);
User user = JSON.parseObject(jsonStr, User.class);
System.out.println(user);
}
@Test
//把json转成Map
public void test09() throws IOException {
String jsonStr = "{\"id\":1,\"username\":\"zs\",\"password\":\"123456\",\"email\":\"zs@163\",\"phone\":\"1386789898\"}";
//1.调⽤JSON.parseObject(String json,Class clazz);
java数组字符串转数组Map map = JSON.parseObject(jsonStr, Map.class);
System.out.println(map);
}
@Test
//使⽤fastjson将json字符串转换成List<JavaBean>
public void test11() {
//使⽤fastjson将json数组的字符串,转换成List<User>
String jsonArr = "[{\"id\":1,\"username\":\"张三\",\"password\":\"123456\",\"email\":\"123456@qq\",\"phone\":\"189********\"},{\"id\":2,\"username\":\"李四\",\"password\":\"654321\",\"email\":\"654321@qq\",\"phone\":\"186******** List<User> userList = JSON.parseArray(jsonArr, User.class);
for (User user : userList) {
System.out.Username());
}
}
fastjson解析复杂json数据:
1、如何从字符串String获得JSONObject对象和JSONArray对象JSONObject jsonObject = new JSONObject ( String str);
JSONArray jsonArray = new JSONArray(String str ) ;
2、如何从JSONArray中获得JSONObject对象
可以把JSONArray当成⼀般的数组来对待,只是获取的数据内数据的⽅法不⼀样JSONObject jsonObject = JSONObject(i) ;
3、获取JSON内的数据
int mid= Int ( "id" ) ;
String String( " courseID") ;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论