Java List<Map<String, Object>>,List<实体> ,实体,Map与 Json 互转

前言:

JSONArray:相当于List<-Object>
JSONObject:相当于Map

1、List> 转为 Json (String)


 String sql=" select * from table";
 List<Map<String, Object>> list= jdbcTemplate.queryForList(sql);
 String str = JSON.toJSONString(list); //此行转换

json格式数据:

	[{
		"formid": "1",
		"fieldname": "ID",
		"xh": 1,
		"bz": "1",
		"fieldzhname": "ID",
		"id": "9b3ef259-4ea5-4137-ad8d-2c4f3534ae12",
		"pkey": 1
	}, {
		"formid": "1",
		"fieldname": "VERSION",
		"xh": 2,
		"bz": "2",
		"fieldzhname": "数据版本",
		"id": "858b7e4d-ec6f-480d-b5e7-dbad91b040eb",
		"pkey": 0
	}]

2、 Json 转为 List>

	String json="[{XXXXXX}]";
	List< Map<String,Object>> listw = toListMap(json); //此行转换
	
	public static List<Map<String, Object>> toListMap(String json){
	       List<Object> list =JSON.parseArray(json);
	       List< Map<String,Object>> listw = new ArrayList<Map<String,Object>>();
	       for (Object object : list){
	           Map<String,Object> ageMap = new HashMap<String,Object>();
	           Map <String,Object> ret = (Map<String, Object>) object;//取出list里面的值转为map
	           listw.add(ret);
	       }
	       return listw;     
	 } 
    

3、 Json 转为 List<实体>

引入的包有:


 import cn.hutool.json.JSONUtil;
 import com.alibaba.fastjson.JSON;
 import cn.hutool.json.JSONArray;
 import com.alibaba.fastjson.JSONObject
 

  List<Entity> list =new LinkedList<>();
  String json="[{XXXXXX}]";
  JSONArray picArray = new JSONArray(json);
  list = JSONUtil.toList(picArray , Entity.class); 
   

4、List<实体> 转为 Json (String)


 import cn.hutool.json.JSONUtil;
 import com.alibaba.fastjson.JSON;
 import cn.hutool.json.JSONArray;
 import com.alibaba.fastjson.JSONObject
 

  // List list 
  //法1:
  String json=JSON.toJSON(list).toString();

  //法2:
  ObjectMapper mapper = new ObjectMapper();
  String json = mapper.writeValueAsString(list);
 

5,Json 转 Map


 import org.json.JSONObject;
 
 String jsonstr={"add":"1","delete":"1","edit":"1"};
 Map map = JSON.parseObject(authstr,Map.class);
 

6,Map转 Json


 import org.json.JSONObject;
 
 HashMap<String,String> map=new HashMap<>();
 map.put("add","1");
 map.put("edit","1");
 map.put("delete","1");
 //法1:
 String jsonstr = new JSONObject(map).toString();


 //法2;
 ObjectMapper mapper = new ObjectMapper();
 String json = mapper.writeValueAsString(map);

7,Json 转 实体


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class jsonDemo {
    public static void main(String[] args) throws IOException {
        String json = "{\"sex\":\"男\",\"age\":\"18\",\"name\":\"张三\"}";
        ObjectMapper objectMapper = new ObjectMapper();
        Person person = objectMapper.readValue(json, Person.class);
        System.out.println(person.toString());
    }
}

8,实体 转 Json

    @Test
    public void test2() throws JsonProcessingException {

        //1.创建Java对象
        Project p=new Project();
        p.setProjectName("新项目123");
        p.setProjectNum("p0001");
        p.setCreatedBy("张三");
        //2.创建Jackson对象 ObjectMapper
        ObjectMapper mapper = new ObjectMapper();
        //3.转换为JSOn
        String json = mapper.writeValueAsString(p);
        System.out.println(json);
    }

9,其他

JSON.parse() 将json字符串解析为一个对象

类型转换:list—string—object

                                                    
List<Map<String,Object>> list=jdbcTemplate.queryForList(sql);
hashmap.put("fielddata",JSON.parse(JSON.toJSONString(list)));

map,修改key值

思路:

1,先将key1的value拿出来
2,删除key1
3,将key1的value放入keynew,


map.put("keynew",map.remove("key1"));

参考文章:

1,List> 与 json 互转

你可能感兴趣的:(Java,json,java,开发语言)