json转换成Map

本文介绍了一种将JSON字符串转换为Java中Map和对象的方法。通过使用net.sf.json库,可以方便地实现JSON字符串到Map及自定义Java对象的转换。这种转换常用于Web应用的数据解析。

1.如果转换的是Map、或者是简单的对象

package com.gc.action;

import java.util.Map;

import net.sf.json.JSONObject;

/**
 * 1.json string 转换为 map
 * 2.json string 转换为 对象
 * @author zhangtengda
 *
 */
public class JsonUtils {
    public static void main(String[] args) {
        String jsonStr = "{\"status\":\"0\",\"data\":{\"name\":\"tom\",\"age\":18}}";

        // ================================
        // 1.如果是拿出来最外层的 map
        Map<Object, Object> result = jsonToMap(jsonStr);
        System.out.println(result.get("status"));
        System.out.println(result.get("data"));

        // 2.如果进步一拿到内层的 map
        Map<Object, Object> data = jsonToMap(result.get("data"));
        System.out.println(data.get("name"));
        System.out.println(data.get("age"));
        // ================================

        Long beginTime = System.currentTimeMillis();
        // 3.转换为对象
        String personStr = "{\"id\":12,\"name\":\"mary\"}";
        Person person = jsonToBean(personStr, Person.class);
        System.out.println(person);

        System.out.println("耗时:"  +( System.currentTimeMillis() - beginTime));

    }

    /**
     * json string 转换为 map 对象
     * @param jsonObj
     * @return
     */
    public static Map<Object, Object> jsonToMap(Object jsonObj) {
        JSONObject jsonObject = JSONObject.fromObject(jsonObj);
        Map<Object, Object> map = (Map)jsonObject;
        return map;
    }

    /**json string 转换为 对象
     * @param jsonObj
     * @param type
     * @return
     */
    public  static <T>  T jsonToBean(Object jsonObj, Class<T> type) {
        JSONObject jsonObject = JSONObject.fromObject(jsonObj);
        T obj =(T)JSONObject.toBean(jsonObject, type);
        return obj;
    } 

}

转载于:https://www.cnblogs.com/zhao-shan/p/9045367.html

要将JSON转换Map,有几种不同的方法可以实现。 第一种方法是使用JSONArray类,首先确保你已经将JSON数据填充到JSONArray对象中。然后,你可以使用以下代码将其转换MapJSONArray jsonArray = new JSONArray(); // 填充初始数据,此处过程省略 List<JSONObject> jsonObjectList = jsonArray.toJavaList(JSONObject.class); Map<Integer, String> map = jsonObjectList.stream() .filter(Objects::nonNull) .collect(Collectors.toMap(item -> item.getInteger("flagType"), item -> item.getString("flagIcon"))); 第二种方法是仍然使用JSONArray类,首先也要确保你已经将JSON数据填充到JSONArray对象中。然后,你可以使用以下代码将其转换MapJSONArray jsonArray = new JSONArray(); // 填充初始数据,此处过程省略 Map<Integer, String> map = jsonArray.stream() .filter(Objects::nonNull) .collect(Collectors.toMap( object -> { JSONObject item = (JSONObject) object; return item.getInteger("flagType"); }, object -> { JSONObject item = (JSONObject) object; return item.getString("flagIcon"); } )); 第三种方法是使用JSONObject类和JSONArray类的组合。在这种方法中,你同样需要先将JSON数据填充到JSONArray对象中。然后,你可以使用以下代码将其转换MapJSONArray jsonArray = new JSONArray(); // 填充初始数据,此处过程省略 Map<Integer, String> flagIconMap = new HashMap<>(); if (jsonArray != null && !jsonArray.isEmpty()) { jsonArray.forEach(object -> { if (object == null) { return; } JSONObject jsonObject = (JSONObject) object; if (jsonObject.getInteger("flagType") == null) { return; } flagIconMap.put(jsonObject.getInteger("flagType"), jsonObject.getString("flagIcon")); }); } 这些方法中的每一种都可以将JSON转换Map,具体使用哪种方法取决于你的需求和环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值