将json串如下:
{
"1":[{"id":001,"age":20,"type":2,"code":"101","password":123456}, {"id":002,"age":22,"type":1,"code":"102","password":123456}],
"2":[{"id":003,"age":23,"type":1,"code":"103","password":123456}]
}
转换为:Map<String, List<User>> 类型
1. 思路就是,
json
字符串其实总得来说就是一个
key-value
的形式,应该是满足了最终想要的
Map<String, List<Unit>>
的结构了,所以直接一个
Collectors.toMap
就搞定啦
Map<String, List<User>> result = JSONObject.parseObject(s)
.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> JSONObject.parseArray(String.valueOf(entry.getValue()), User.class)));
2. 自己封装一个将 json 解析为常用数据类型的工具类
public static Map<String, Object> jsonToMap(JSONObject json) throws JSONException {
Map<String, Object> retMap = new HashMap<String, Object>();
if(json != JSONObject.NULL) {
retMap = toMap(json);
return retMap;
public static Map<String, Object> toMap(JSONObject object) throws JSONException {
Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> keysItr = object.keys();
while(keysItr.hasNext()) {
String key = keysItr.next();
Object value = object.get(key);
if(value instanceof JSONArray) {
value = toList((JSONArray) value);
else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
map.put(key, value);
return map;
public static List<Object> toList(JSONArray array) throws JSONException {
List<Object> list = new ArrayList<Object>();
for(int i = 0; i < array.length(); i++) {
Object value = array.get(i);
if(value instanceof JSONArray) {
value = toList((JSONArray) value);
else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
list.add(value);
return list;
将json串如下:{"1":[{"id":001,"age":20,"type":2,"code":"101","password":123456}, {"id":002,"age":22,"type":1,"code":"102","password":123456}],"2":[{"id":003,"age":23,"type":1,"code":"103","password":123...
listData.add(map);
Gson gson = new Gson();
ObjectMapper objectMapper = new ObjectMapper();
List> newMap=objectMapper.r
* 传入参数JSON格式校验与转换
* @param data 传入JSON格式实字符串
* @return Map<String,Object> 返回JSON转换成Map数据
public st...
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
import java.util.Map;
* Created by zkn on 2016/8/22.
public class JsonToM.
List<Map<String, Object>> classifyLh = new ArrayList<>();
Map<String,String> paramInfo = new HashMap<>();
paramInfo.put("batchParam", JSON.toJSONString(classifyLh));//转换
JSON 转L
public static List<Map<String, Object>> toListMap(String json){
List<Object> list =JSON.parseArray(json);
List< Map<String,Object>> listw = new ArrayLi..
JsonObject和JsonObject转化
1.可以直接将JsonObject对象赋给JsonObject对象
public static void main(String[] args)
String propertyPairs = "{\"id\":1,\"name\":\"nihh\"}";
JSONObject property = JSONObject.fromObject(propertyPairs);
Map<String, Object> propetyMa
将list字符串转ArrayList<Map<String,Object>>(需json字符串转map方法)
public static ArrayList<Map<String,Object>> strislist(Object str){
StringBuffer strb=new StringBuffer((String) str);
List<String> list=new ArrayList<>
利用ObjectMapper类,可将json字符串转成Lis<Map<String,Object>>。
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
String json = "{ \"1\":"...
// Json数组形式的字符串转为Map数组
String strArr = "[{"name": "xx", "age": "16"},{"name": "yy", "age": "18"}]";
//第一种方式
List<Map<String,String>> listObjectFir = (List<Map<String,String...
* 将json格式的字符串解析成Map对象
* json格式:{"name":"admin","retries":"3fff","testname"
* :"ddd","testretries":"fffffffff"}
private HashMap toHashMap(JSONObject jsonObject) {
HashMap data = new HashM
前提:使用阿里巴巴的JSON包
String json = ""; //JSON字符串
Map map = JSON.parseObject(json, Map.class);
//遍历 Map 使用迭代器
Iterator it = map.keySet().iterator();
while(it.hashNext()){
String values = map.get(...
Jackson是一个Java库,可以将
JSON字符
串转换为Java对象,例如
List<
Map>。可以使用
ObjectMapper类的readValue()方法将
JSON字符
串转换为
List<
Map>。
示例代码:
ObjectMapper
mapper = new
ObjectMapper();
String jsonString = "{\"name\":\"John\",\"age\":30}";
List<
Map<
String,
Object>>
jsonList =
mapper.readValue(
jsonString, new TypeReference<
List<
Map<
String,
Object>>>(){});
jsonString 是你要转换的
json字符串
jsonList是经过转换之后的
List<
Map>
weixin_43896630: