• java代码(处理json串)


    package test;

    import com.alibaba.fastjson.JSON;

    import com.alibaba.fastjson.JSONObject;

    public class DealJsonString

    {

        /* JSON文本parseJSONObject或者JSONArray

        public static final Object parse(String text)

        JSON文本parseJSONObject

        public static final JSONObject parseObject(String text)

        JSON文本parseJSONArray

        public static final JSONArray parseArray(String text)

        JavaBean转换为JSONObject或者JSONArray

        public static final Object toJSON(Object javaObject)

        JSON文本parseJavaBean

        public static final T parseObject(String text, Class clazz)

        JSON文本parseJavaBean集合

        public static final List parseArray(String text, Class clazz)

        JavaBean序列化为JSON文本

        public static final String toJSONString(Object object);

        JavaBean序列化为带格式的JSON文本

        public static final String toJSONString(Object object, boolean prettyFormat)

        */

        public static vopersonId main(String[] args)

        {

            String jsonStr1 = "{"person":{"personId":100127,"phone":"13811821600"}}";

            String jsonStr2 = "{"code": 0,"data": [{"personType": 2,"housepersonId": 201010091001,"type": 3,"personId": 100684,},{"personType": 1,"housepersonId": 201010091002,"type": 2,"personId": 201010091003,}],"dataExtra": null,"msg": "OK"}";

            String jsonStr3 = "{"data":{"pictureList":[{"bigPictureFilePath":"http://ip:port//vpersonIdeoFile/201001021001.jpg","mpersonIddlePictureFilePath":"http://ip:port//vpersonIdeoFile/201001021002.jpg","smallPictureFilePath":"http://ip:port//vpersonIdeoFile/201001021003.jpg"}],"vpersonIdeoFilePaht":"http://ip:port//vpersonIdeoFile/201001021001.mp4"},"code":0,"msg":"成功"}";

            String jsonStr4 = "{"listResult": {"curPage": 1,"resultCount": 2,"totalCount": 32,"pageSize": 10,"start": 0,"end": 10,"orderName": "personId","sortedBy": "desc","data": null,"page": true},"data": [{"personId": 201001021001,"status": 2,"content": "内容2","category": 3},{"personId": 201001021002,"status": 2,"content": null,"category": 3},{"personId": 201001021003,"status": 1,"content": null,"category": 2},{"personId": 201001021004,"status": 2,"content": "内容3","category": 3}],"status": 0,"msg": null}";

            String jsonStr5 = "{"listResult": {"curPage": 1},"data": {"houses": [{"personId": 201001021001,"status": 2,},{"personId": 201001021101,"status": 3},{"personId": 201001021031,"status": 3},{"personId": 201001021303,"status": 3}],"overall": {"payMoney": 0,"houseNum": 2}},"status": 0,"msg": null}";

            String jsonStr6 = "{"listResult":{"curPage":1,"resultCount":0,"totalCount":0,"pageSize":20,"start":0,"end":20,"orderName":"personId","sortedBy":"desc","data":null,"page":true},"data":{"houses":[],"overall":{"payMoney":0.0,"houseNum":0}},"status":0,"msg":null}";

            String jsonStr7 = "{"code":0,"msg":"0","data":{"paypayMoney":null}}";

            //如果是对象用 getJSONObject("data"); 如果是array getJSONArray"data") 单独的字段用 getString("")获取;

            // 将相应结果转换为json对象

            JSONObject jsonResponse = JSON.parseObject(jsonStr7);

            // 获取data json对象

            JSONObject jsonData = jsonResponse.getJSONObject("data");

            float paypayMoney = 0.0f;

            try

            {

                String tempPaypayMoney = jsonData.getString("paypayMoney");

                paypayMoney = Float.parseFloat(tempPaypayMoney);

            }

            catch (Exception e)

            {

                paypayMoney = 0.0f;

            }

            System.out.println(String.valueOf(paypayMoney));

        }

    }

  • 相关阅读:
    【重点推荐】五美凡生论
    语言哲学宣言2018
    四要同环图
    知识分子必须毫不留情反对一切“教养阶层”
    世界上任何一件事的五个模块
    Web 在线制表工具稳定吗?和桌面报表工具对比哪个好用?
    Web 在线制表工具稳定吗?和桌面报表工具对比哪个好用?
    有没有简单易用的数据挖掘工具?
    BI、OLAP、多维分析、CUBE 这几个词是什么关系?
    传说中的中国复杂报表都长什么样?有什么特点?
  • 原文地址:https://www.cnblogs.com/NiceTime/p/6758810.html
Copyright © 2020-2023  润新知