• fastjson与各类型的转换


    参考:

    https://www.cnblogs.com/ceshi2016/p/7381478.html

    http://www.cnblogs.com/goody9807/p/4244862.html

     https://www.cnblogs.com/DreamDrive/p/5778959.html

    https://www.cnblogs.com/janson071/p/9646678.html 多层嵌套

    /**字符串转Json数组并解析**/

    try {

    JSONArray array =  JSONArray.parseArray(line));
             String[] fields = {"lat", "lng", "poi_id", "poi_name", "poi_type", "roadidx"};
             if (null != array) {
                 Iterator<Object> iter = array.iterator();
                 while (iter.hasNext()) {
                     JSONObject json = (JSONObject)iter.next();
                     for (int i = 0; i < fields.length; i++) {
                         System.out.println(json.getString(fields[i]));
                     }
                 }
             }

    }catch (JSONException e) {
      System.err.println("json paese exception = " + jsonStr);
    }

    /**map转Json对象**/
    Map<String, String> map = new HashMap<String, String>();
    JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(map));
    /**Json对象转map**/
    Map<String, Object> map2 = JSONObject.toJavaObject(jsonObject, Map.class);


    /**将List转换成JSONArray**/
    List<String> list = new ArrayList<String>();
    JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(list));
    /**将JSONArray转换成List**/
    http://www.cnblogs.com/goody9807/p/4244862.html


    /**字符串转Json对象**/
    String jsonInfo = "";
    JSONObject jsonObject2 = JSONObject.parseObject(jsonInfo);
    /**字符串转Json对象**/
    String jsonStrng = JSON.toJSONString(jsonObject2);

    /**对JSONObject中的指定字段重新赋值**/

    JSONObject posJson = new JSONObject();

    posJson.put("op", "in");

    /**其它**/

    public class JsonDemo {
        public static void main(String[] args) {
    
            //1.json字符串转换为对象
            String jsonString="{'name':'42313123','id':'2345','age':12}";
            JSONObject jsonObject = JSONObject.parseObject(jsonString);
            String id = jsonObject.getString("id");
            System.out.println(id);
    
            //2. JSONObject转化成自定义类对象
            PeoplePo peoplePo1 = JSONObject.parseObject(jsonString, PeoplePo.class);
            System.out.println(peoplePo1);
    
            //3. JSONObject转化成Map集合
            Map map = JSONObject.parseObject(jsonString, Map.class);
            System.out.println(map);
    
            //4. 自定义对象转化成json格式的字符串
            PeoplePo peoplePo = new PeoplePo();
            peoplePo.setId("1");
            peoplePo.setAge(11);
            peoplePo.setName("LH");
            String peopleJson = JSON.toJSONString(peoplePo);
            System.out.println(peopleJson);
    
            //5. String类型转化成JSONObject;
            String str = "{"result":"success","message":"成功!"}";
            JSONObject jsonObject1 = JSONObject.parseObject(str);
            System.out.println(jsonObject1);
    
            //6. JSONObject转化成JSONArray的两种方式
            String str1 = "{"result":"success","message":"成功!","data":[{"name":"Tom","age":"20"}]}";
            JSONObject jsonToArray = JSONObject.parseObject(str1);
            //方式一
            JSONArray data = jsonToArray.getJSONArray("data");
            System.out.println(data);
            //方式二
            JSONArray jsonArray = JSONArray.parseArray(jsonToArray.getString("data"));
            System.out.println(jsonArray);
    
            //7. jsonArray转化成JSONObject并取出其中的元素数据
            JSONObject o = (JSONObject) jsonArray.get(0);
            String name = o.getString("name");
            System.out.println(o);
            System.out.println(name);
            System.out.println(jsonArray.toString());
        }
    }
    //转自 https://blog.csdn.net/lh19960914/article/details/87861308
  • 相关阅读:
    Performance and Design
    返回数组中不重复的元素
    IE的button元素bug
    (转)Google Closure: 糟糕的JavaScript
    Why do we have an IMG element?
    About this and that
    C#中的Attribute
    C#检查字体是否存储,以及安装
    ZipFile压缩文件后,解压文件后有多层目录的处理方法
    Office系列在线预览
  • 原文地址:https://www.cnblogs.com/ying-chease/p/9188191.html
Copyright © 2020-2023  润新知