1.fastjson中List和JSONArray的相互转换
(1)List
转换为JSONArray
List<T> list = new ArrayList<T>();
JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
(2)JSONArray
转换为List
JSONArray array = new JSONArray();
List<EventColAttr> list = JSONObject.parseArray(array.toJSONString(), EventColAttr.class);
2.实例
创建一个User.java
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
}
(1)List
转换为JSONArray
// list--->Json字符串-->JSONArray
@Test
public void test2(){
//准备工作
List<User> list = new ArrayList<User>();
User user1 = new User();
user1.setName("zhangsan");
user1.setAge(14);
User user2 = new User();
user2.setName("lisi");
user2.setAge(24);
list.add(user1);
list.add(user2);
//转换为Json字符串
String string = JSON.toJSONString(list);
System.out.println("String为:"+string+",其类型为:"+string.getClass());
//转换为JSONArray
JSONArray array = JSON.parseArray(JSON.toJSONString(list));
System.out.println("JSONArray为:"+array+",其类型为:"+array.getClass());
}
运行结果:
String为:[{"age":14,"name":"zhangsan"},{"age":24,"name":"lisi"}],其类型为:class java.lang.String
JSONArray为:[{"name":"zhangsan","age":14},{"name":"lisi","age":24}],其类型为:class com.alibaba.fastjson.JSONArray
(2)JSONArray
转换为List
// JSONArray--->Json字符串 ----->list
@Test
public void test3(){
//准备工作
List<User> list = new ArrayList<User>();
User user1 = new User();
user1.setName("zhangsan");
user1.setAge(14);
User user2 = new User();
user2.setName("lisi");
user2.setAge(24);
list.add(user1);
list.add(user2);
JSONArray array = JSON.parseArray(JSON.toJSONString(list));
//转换为Json字符串
String string = array.toJSONString(array);
System.out.println("String为:"+string+",其类型为:"+string.getClass());
//转换为list
List<User> list2 = JSON.parseArray(string, User.class);
System.out.println("list为:"+list2+",其类型为:"+list2.getClass());
}
运行结果:
String为:[{"name":"zhangsan","age":14},{"name":"lisi","age":24}],其类型为:class java.lang.String
list为:[User [name=zhangsan, age=14], User [name=lisi, age=24]],其类型为:class java.util.ArrayList
============
package com.testjson;
import jdk.nashorn.internal.objects.annotations.Constructor;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Hello {
private String id;
private String name;
private String email;
private String address;
}
package com.testjson;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class World {
private String price;
private String count;
private String total;
}
package com.testjson;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class User {
private String name;
private int age;
}
package com.testjson;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class JsonOne {
public static void main(String[] args) {
Hello hello=new Hello("1","hello1","hello@123","hellokitty");
Hello hello1=new Hello("2","hello2","hello2@123","hellokitty2");
World world=new World("price","count","total");
JSONObject o = (JSONObject)JSONObject.toJSON(hello);
JSONObject o1 = (JSONObject)JSONObject.toJSON(world);
//对象转化为jsonString
String string1 = JSON.toJSONString(hello);
String string2 = JSON.toJSONString(world);
System.out.println(string1);
System.out.println(string2);
//合并两个json对象
o.putAll(o1);
System.out.println("合并两个jsonObject->"+o.toString());
//合并两个json字符串,将字符串转化为jsonObject后再合并(如果两个jsonObject有相同的key会被覆盖)
JSONObject jsonObject1 = JSON.parseObject(string1);
JSONObject jsonObject2 = JSON.parseObject(string2);
jsonObject1.putAll(jsonObject2);
System.out.println("合并两个json字符串->"+jsonObject1);
//jsonString<-相互转化->Map
Map map = JSON.parseObject(string1, Map.class);
//使用keySet遍历
for (Object ignored :map.keySet()) {
System.out.println("key->"+ignored+" "+"value->"+map.get(ignored));
}
//使用Map.Entry遍历
System.out.println("转化成map");
for (Object map1: map.entrySet()){
System.out.println(((Map.Entry)map1).getKey()+" "+((Map.Entry)map1).getValue());
}
JSONObject jsonObject=new JSONObject(map);
System.out.println("map->json:"+jsonObject.toString());
//jsonString<-相互转化->JavaBean
World world1 = JSON.parseObject(string2, World.class);
System.out.println("world1.getPrice()->"+world1.getPrice());
//jsonString<-相互转化->List
List list_=new LinkedList();
list_.add(hello);
list_.add(world);
for (int i = 0; i < list_.size(); i++) {
System.out.println("list->"+list_.get(i));
}
String jsonString = JSON.toJSONString(list_);
//JSONArray jsonArray = JSONArray.parseArray(jsonString);
System.out.println("list(hello/world)->"+jsonString);
//jsonString<-相互转化->JsonArray
JSONArray jsonArray = JSON.parseArray(jsonString);
List linkedList = jsonArray.toJavaObject(List.class);
for (int i = 0; i < linkedList.size(); i++) {
System.out.println("linkedList->"+linkedList.get(i));
}
net.sf.json.JSONArray jsonArray_ = net.sf.json.JSONArray.fromObject(jsonString);
List list = jsonArray_.toList(jsonArray_);
for (int i = 0; i < list.size(); i++) {
System.out.println("list->"+list.get(i));
}
}
}
输出结果
{"address":"hellokitty","email":"hello@123","id":"1","name":"hello1"}
{"count":"count","price":"price","total":"total"}
合并两个jsonObject->{"total":"total","address":"hellokitty","price":"price","name":"hello1","count":"count","id":"1","email":"hello@123"}
合并两个json字符串->{"total":"total","address":"hellokitty","price":"price","name":"hello1","count":"count","id":"1","email":"hello@123"}
key->address value->hellokitty
key->name value->hello1
key->id value->1
key->email value->hello@123
转化成map
address hellokitty
name hello1
id 1
email hello@123
map->json:{"address":"hellokitty","name":"hello1","id":"1","email":"hello@123"}
world1.getPrice()->price
list->Hello(id=1, name=hello1, email=hello@123, address=hellokitty)
list->World(price=price, count=count, total=total)
list(hello/world)->[{"address":"hellokitty","email":"hello@123","id":"1","name":"hello1"},{"count":"count","price":"price","total":"total"}]
linkedList->{"address":"hellokitty","name":"hello1","id":"1","email":"hello@123"}
linkedList->{"total":"total","price":"price","count":"count"}
list->net.sf.ezmorph.bean.MorphDynaBean@13eb8acf[
{address=hellokitty, name=hello1, id=1, email=hello@123}
]
list->net.sf.ezmorph.bean.MorphDynaBean@51c8530f[
{total=total, price=price, count=count}
]
=====================================
package com.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.entity.User;
import java.util.ArrayList;
import java.util.List;
public class UserService {
public static void main(String[] args) {
User user=new User();
user.setAge(18);
user.setName("张三");
User user1=new User();
user1.setAge(20);
user1.setName("李四");
List<User>list=new ArrayList<User>();
list.add(user);
list.add(user1);
JSONArray jsonArray = (JSONArray)JSONObject.toJSON(list);
Object o = jsonArray.get(1);
System.out.println(o);
List<User> list1 = JSONObject.parseArray(jsonArray.toString(), User.class);
System.out.println(list1);
}
}