直接用代码来演示吧
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
</dependency>
pet.class
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Pet {
private String petId;
private String petName;
private Integer breed;
private List<String> objectList;
@Override
public String toString() {
return "Pet{" +
"petId='" + petId + '\'' +
", petName='" + petName + '\'' +
", breed=" + breed +
", objectList=" + objectList +
'}';
}
}
pet.class
public class demo {
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.springboot.domain.RelBillIssue;
import com.springboot.entity.Pet;
import lombok.SneakyThrows;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.assertj.core.util.Strings;
import org.json.JSONException;
import org.junit.Test;
import org.springframework.test.annotation.IfProfileValue;
import org.springframework.util.CollectionUtils;
@Test
public void hashMapToList3(){
//hashmap
HashMap hashMap = new HashMap();
hashMap.put("petId","aaa");
hashMap.put("petName","petName");
hashMap.put("breed",1111);
//jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.put("petId","aaa");
jsonObject.put("petName","petName");
jsonObject.put("breed",1111);
List<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
jsonObject.put("objectList",list);
hashMap.put("objectList",list);
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("petId","aaa");
jsonObject1.put("petName","petName");
jsonObject1.put("breed",1111);
List<String> list1 = new ArrayList<>();
list1.add("aaa");
list1.add("bbb");
list1.add("ccc");
jsonObject1.put("objectList",list1);
Pet pet = JSON.toJavaObject(jsonObject,Pet.class);
//hashMap转jsonObject
JSONObject fromHashMap = (JSONObject) JSON.toJSON(hashMap);
Pet pet1 = JSON.toJavaObject(fromHashMap,Pet.class);
System.out.println("jsonObjectPet:"+pet);
System.out.println("hashMapPet:"+pet1);
}
后台打印结果
jsonObjectPet:Pet{petId='aaa', petName='petName', breed=1111, objectList=[aaa, bbb, ccc]}
hashMapPet:Pet{petId='aaa', petName='petName', breed=1111, objectList=[aaa, bbb, ccc]}
简而言之
hashMap转jsonObject:
import com.alibaba.fastjson.JSON;
JSONObject fromHashMap = (JSONObject) JSON.toJSON(hashMap);
hashMap转json字符串:
import com.alibaba.fastjson.JSON;
String jsonStr = JSON.toJSONString(hashMap);
hashMap转实体类:
import com.alibaba.fastjson.JSON;
JSONObject fromHashMap = (JSONObject) JSON.toJSON(hashMap);
Pet pet1 = JSON.toJavaObject(fromHashMap,Pet.class);
或者
Pet pet1 = JSON.toJavaObject((JSON) JSON.toJSON(hashMap),Pet.class);
jsonObject转实体类:
import com.alibaba.fastjson.JSON;
Pet pet = JSON.toJavaObject(jsonObject,Pet.class);
jsonObject转hashMap:
import com.alibaba.fastjson.JSON;
HashMap hashMap1 = (HashMap) JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<Map<String, String>>(){});
实体类转jsonObject:
import com.alibaba.fastjson.JSON;
JSONObject jsonObject2 = (JSONObject) JSON.toJSON(pet);
实体类转json字符串:
import com.alibaba.fastjson.JSON;
String jsonStr = JSON.toJSONString(pet);
实体类转hashMap:
import com.alibaba.fastjson.JSON;
HashMap hashMap2 =(HashMap)JSONObject.parseObject(JSON.toJSON(pet).toString(), new TypeReference<Map<String, String>>(){});
jsonArray转实体类:
import com.alibaba.fastjson.JSON;
List<Pet> listA = JSONArray.parseArray(jsonArray.toJSONString(),Pet.class);
字符串转List实体类:
import com.alibaba.fastjson.JSON;
String text = "[{"petName":"petName","petId":"aaa","breed":1111,"objectList":["aaa","bbb","ccc"]}]";
List<Pet> list = JSONArray.parseArray(text,Pet.class);
List实体类转JSONArray :
import com.alibaba.fastjson.JSON;
JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));