zoukankan      html  css  js  c++  java
  • 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));

        }

    }

  • 相关阅读:
    php 高并发
    mysql 基础明细
    关于高并发和秒杀系统,你知道的和不知道的一些事
    carbon
    自定义tarbar
    学习小参考
    lnmp1.4,400,500,错误
    PHPSTORM+Thinkphp3.2模板标签替换Thinkphp5.1公式
    Thinkphp5.1手册太简单,有的功能用起来不确定结果是否和预料的一样,顾整理记录
    CentOS7 最小化安装vmware-tools
  • 原文地址:https://www.cnblogs.com/NiceTime/p/6758810.html
Copyright © 2011-2022 走看看