首页 > Java > java教程 > 正文

Java中Json的处理方法有哪些

PHPz
发布: 2023-05-16 18:10:06
转载
3107人浏览过

    Java Json的各种处理

    一、net.sf.json

    1、json转map

    JSONObject jsonObject = JSONObject.fromObject(jsonStr);
    Map<String,Object> map = new HashMap<>();
    map.put("code",jsonObject .getInt("code"));
    登录后复制

    2、Json转实体

    JSONObject jsonObject = JSONObject.fromObject(jsonStr);
    ArticleForm articleForm = (ArticleForm) JSONObject.toBean(jsonObject , ArticleForm.class);
    登录后复制

    如果实体中带有List字段,需要指定泛型

    Map<String, Class> classMap = new HashMap<String, Class>();
    classMap.put("keywords", String.class);
    ArticleForm articleForm = (ArticleForm) JSONObject.toBean(data.getJSONObject(i), ArticleForm.class,classMap);
    登录后复制

    3、Json转集合

    List<ArticleForm> list = new ArrayList<>();
    JSONArray data = jsonObject.getJSONArray("data");
    if (errorCode == 0 && data != null && !data.isEmpty()) {
        for (int i = 0; i < data.size(); i++) {
            Map<String, Class> classMap = new HashMap<String, Class>();
            classMap.put("keywords", String.class);
            ArticleForm articleForm = (ArticleForm) JSONObject.toBean(data.getJSONObject(i), ArticleForm.class,classMap);
            list.add(articleForm);
        }
    }
    登录后复制

    另外一种:

    立即学习Java免费学习笔记(深入)”;

    Find JSON Path Online
    Find JSON Path Online

    Easily find JSON paths within JSON objects using our intuitive Json Path Finder

    Find JSON Path Online 30
    查看详情 Find JSON Path Online
    List<ArticleForm> list = new ArrayList<>();
    JSONArray data = jsonObject.getJSONArray("data");
    if (errorCode == 0 && data != null && !data.isEmpty()) {
        Map<String, Class> classMap = new HashMap<String, Class>();
        classMap.put("keywords", String.class);
        list  = (List<ArticleForm>) JSONArray.toArray(data, ArticleForm.class,classMap);
    }
    登录后复制

    二、com.alibaba.fastjson

    1、json转map

    JSONObject jsonObject = JSON.parseObject(jsonStr);
    Map<String,Object> map = new HashMap<>();
    map.put("code",jsonObject .getInt("code"));
    登录后复制

    2、Json转实体

    ArticleForm articleForm = JSON.parseObject(jsonStr, new TypeReference<ArticleForm>() {});
    登录后复制

    3、Json转集合

    List<ArticleForm> list = JSON.parseObject(jsonStr,new TypeReference<ArrayList<ArticleForm>>() {});
    登录后复制

    Java常用json处理

    // String和json的互相转换
            String str = "{\"status\":200,\"message\":\"\",\"data\":{\"KmList\":[\"总分\",\"语文\",\"数学\",\"英语\",\"道德与法治\",\"科学基础\"]}}";
            System.out.println("str:"+str);
    //        JSONArray arrays = JSON.parseArray(str);        // string转jsonArray
            JSONObject jsonObject = JSON.parseObject(str);    // string转jsonObject
            System.out.println("jsonObject:"+jsonObject);
            String s = jsonObject.toJSONString();             // json(object和Array相同)转string
      
            // json转list<Object>或者object
            String str1 = "[\"总分\",\"语文\",\"数学\",\"英语\",\"道德与法治\",\"科学基础\"]";
            List<String> list = JSON.parseArray(str1, String.class); // json转list集合,将String.class改成其他对象.class即可
            System.out.println("list:"+JSON.toJSONString(list));
            String s1 = JSON.parseObject(JSON.toJSONString("语文"), String.class);       // json转对象,将String.class改成其他对象.class即可
            System.out.println("s1:"+s1);
            // object转字符串后即可转jsonObject或者jsonArray
     
            // json和map
            Map<String, Object> map = new HashMap<>();
            map.put("xAxis","11");
            map.put("yAxis","2222");
            String json = JSON.toJSONString(map);//map转String
            System.out.println("json:"+json);
            Map<String, Object>  map1 = JSON.parseObject(json, Map.class);     // 转List<Map> parserArray即可
            System.out.println("map1:"+map1);
    登录后复制

    以上就是Java中Json的处理方法有哪些的详细内容,更多请关注php中文网其它相关文章!

    相关标签:
    java速学教程(入门到精通)
    java速学教程(入门到精通)

    java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

    下载
    来源:亿速云网
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    最新问题
    开源免费商场系统广告
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送
    PHP中文网APP
    随时随地碎片化学习

    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号