jsonObeject与hashMap与实体类之间的互转

直接用代码来演示吧

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转实体类:
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>>(){});

上一篇:<小甲鱼>C++实例练习20—猫狗问题(虚方法)


下一篇:java-多态