HashMap ArrayList 및 List 객체 변환

6000 단어
public static void main(String[] args) {
        List list = new ArrayList<>();
    
        HashMap map = new HashMap();
        map.put("name", "zhou");
        map.put("age", 20);
        map.put("Address", "hubei");
        map.put("career", "student");
        list.add(map);
        
        HashMap map1 = new HashMap();
        map1.put("name", "zhangsan");
        map1.put("age", 30);
        map.put("Address", "wuhan");
        map.put("career", "teacher");
        list.add(map1);
        
        System.out.println(list);
        JSONArray result = JSONArray.fromObject(list);
        
        List jsonDtosList = (List) JSONArray.toCollection(result, Person.class);
        
        System.out.println(jsonDtosList);

net.sf.json.JSONArray;의
JSONArray.fromObject(list);      hashMap   List  
JSONArray  (  JSONObject), 

List jsonDtosList = (List) JSONArray.toCollection(result, Person.class);  
JSONArray    。
。 null
package stream;

public class Person {
    private String name;
    private int age;
    private String address;
    
    
    
    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;
    }



    public String getAddress() {
        return address;
    }



    public void setAddress(String address) {
        this.address = address;
    }
    
    



    /*
     * public Person(String name, int age) {
     * 
     * this.name = name; this.age = age; }
     */
    

}

 
 : HashMap   JSONObject:
       map.put("title",title);
map.put("content",text);
       net.sf.json.JSONObject jsonObject= JSONObject.fromObject(map);

 :HashMap   JSONObject:
map.put("title",title);
map.put("content",text);
com.alibaba.fastjson.JSONObject.JSONObject jsonObject= JSONObject.parseObject(JSON.toJSONString(map));


 :    hutool   JSONObject   json  JSONObject  , HashMap  ,  map.putAll  HashMap 。
jsonObject.get(String key) 。
        String strq="{\"pfid\":\"1164806502843756545\",\"nickName\":\" \"}";
        cn.hutool.json.JSONObject jsonObject = cn.hutool.json.JSONUtil.parseObj(strq);
        Map data = new HashMap();
        data.putAll(jsonObject);
        
        System.out.println(data);
        System.out.println(data.get("pfid"));
        String nickName = (String) jsonObject.get("nickName");
        System.out.println(nickName);

 
전재 대상:https://www.cnblogs.com/z360519549/p/11385118.html

좋은 웹페이지 즐겨찾기