Android中解析不规则json的理解(基于Gson)

json="{
    "result": "1",
    "message": "成功!",
    "list": {
        "0": {
            "glj_price": 200,
            "lrj_price": 895.98,
            "fdj_price": 707.94,
            "allmoney": 1803.92,
            "rq": "2017-08"
        },
        "7": {
            "glj_price": 0,
            "lrj_price": 0,
            "fdj_price": 60,
            "allmoney": 60,
            "rq": "2016-12"
        }, "70": {
            "glj_price": 0,
            "lrj_price": 0,
            "fdj_price": 60,
            "allmoney": 60,
            "rq": "2016-12"
        }
    },
    "teams_zprice": "24474.00"

}";

按常理 直接gsonformat 解决.

public class NOO {

    /**
     * result : 1
     * message : 成功!
     * list : {"0":{"glj_price":200,"lrj_price":895.98,"fdj_price":707.94,"allmoney":1803.92,"rq":"2017-08"},"7":{"glj_price":0,"lrj_price":0,"fdj_price":60,"allmoney":60,"rq":"2016-12"}}
     * teams_zprice : 24474.00
     */

    private String result;
    private String message;
    private ListBean list;
    private String teams_zprice;

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public ListBean getList() {
        return list;
    }

    public void setList(ListBean list) {
        this.list = list;
    }

    public String getTeams_zprice() {
        return teams_zprice;
    }

    public void setTeams_zprice(String teams_zprice) {
        this.teams_zprice = teams_zprice;
    }

    public static class ListBean {
        /**
         * 0 : {"glj_price":200,"lrj_price":895.98,"fdj_price":707.94,"allmoney":1803.92,"rq":"2017-08"}
         * 7 : {"glj_price":0,"lrj_price":0,"fdj_price":60,"allmoney":60,"rq":"2016-12"}
         */

        @SerializedName("0")
        private _$0Bean _$0;
        @SerializedName("7")
        private _$7Bean _$7;

        public _$0Bean get_$0() {
            return _$0;
        }

        public void set_$0(_$0Bean _$0) {
            this._$0 = _$0;
        }

        public _$7Bean get_$7() {
            return _$7;
        }

        public void set_$7(_$7Bean _$7) {
            this._$7 = _$7;
        }

        public static class _$0Bean {
            /**
             * glj_price : 200
             * lrj_price : 895.98
             * fdj_price : 707.94
             * allmoney : 1803.92
             * rq : 2017-08
             */

            private int glj_price;
            private double lrj_price;
            private double fdj_price;
            private double allmoney;
            private String rq;

            public int getGlj_price() {
                return glj_price;
            }

            public void setGlj_price(int glj_price) {
                this.glj_price = glj_price;
            }

            public double getLrj_price() {
                return lrj_price;
            }

            public void setLrj_price(double lrj_price) {
                this.lrj_price = lrj_price;
            }

            public double getFdj_price() {
                return fdj_price;
            }

            public void setFdj_price(double fdj_price) {
                this.fdj_price = fdj_price;
            }

            public double getAllmoney() {
                return allmoney;
            }

            public void setAllmoney(double allmoney) {
                this.allmoney = allmoney;
            }

            public String getRq() {
                return rq;
            }

            public void setRq(String rq) {
                this.rq = rq;
            }
        }

        public static class _$7Bean {
            /**
             * glj_price : 0
             * lrj_price : 0
             * fdj_price : 60
             * allmoney : 60
             * rq : 2016-12
             */

            private int glj_price;
            private int lrj_price;
            private int fdj_price;
            private int allmoney;
            private String rq;

            public int getGlj_price() {
                return glj_price;
            }

            public void setGlj_price(int glj_price) {
                this.glj_price = glj_price;
            }

            public int getLrj_price() {
                return lrj_price;
            }

            public void setLrj_price(int lrj_price) {
                this.lrj_price = lrj_price;
            }

            public int getFdj_price() {
                return fdj_price;
            }

            public void setFdj_price(int fdj_price) {
                this.fdj_price = fdj_price;
            }

            public int getAllmoney() {
                return allmoney;
            }

            public void setAllmoney(int allmoney) {
                this.allmoney = allmoney;
            }

            public String getRq() {
                return rq;
            }

            public void setRq(String rq) {
                this.rq = rq;
            }
        }
    }
}

但是有一个细节 是  list  中  项数目不定   而且  每一个 项对应一个object

如果上述json 中list  只有 1项  或者  10项  那就不行了   所以只能手动................
最简单的办法   让后台改一下
如果后台不改  那就只能自己写了........

自己建一个类  写上需要的属性.
例如

public class Goods {
    private String id;
    private BBean goods;
    public String getid() {
        return id;
    }

    public void setid(String id) {
        this.id = id;
    }
    public BBean getgoods() {
        return goods;
    }

    public void setgoods(BBean goods) {
        this.goods = goods;
    }
    public static class BBean {
        /**
         * glj_price : 200
         * lrj_price : 895.98
         * fdj_price : 707.94
         * allmoney : 1803.92
         * rq : 2017-08
         */

        public double glj_price;
        public double lrj_price;
        public double fdj_price;
        public double allmoney;
        public String rq;

        public double getGlj_price() {
            return glj_price;
        }

        public void setGlj_price(int glj_price) {
            this.glj_price = glj_price;
        }

        public double getLrj_price() {
            return lrj_price;
        }

        public void setLrj_price(double lrj_price) {
            this.lrj_price = lrj_price;
        }

        public double getFdj_price() {
            return fdj_price;
        }

        public void setFdj_price(double fdj_price) {
            this.fdj_price = fdj_price;
        }

        public double getAllmoney() {
            return allmoney;
        }

        public void setAllmoney(double allmoney) {
            this.allmoney = allmoney;
        }

        public String getRq() {
            return rq;
        }

        public void setRq(String rq) {
            this.rq = rq;
        }
    }
}
记录list 中  key   value
+++++++++++++++++++++++++++++++++
下面是我在百度的基础上改的  注意  里面所有的jsonobject 均为gson包下的  不是原生的 原生应该也能写
==================================
public class MainActivity extends AppCompatActivity {
    String json="{\n" +
            "    \"result\": \"1\",\n" +
            "    \"message\": \"成功!\",\n" +
            "    \"list\": {\n" +
            "        \"0\": {\n" +
            "            \"glj_price\": 200,\n" +
            "            \"lrj_price\": 895.98,\n" +
            "            \"fdj_price\": 707.94,\n" +
            "            \"allmoney\": 1803.92,\n" +
            "            \"rq\": \"2017-08\"\n" +
            "        },\n" +
            "        \"7\": {\n" +
            "            \"glj_price\": 0,\n" +
            "            \"lrj_price\": 0,\n" +
            "            \"fdj_price\": 60,\n" +
            "            \"allmoney\": 60,\n" +
            "            \"rq\": \"2016-12\"\n" +
            "        }, \"70\": {\n" +
            "            \"glj_price\": 0,\n" +
            "            \"lrj_price\": 0,\n" +
            "            \"fdj_price\": 60,\n" +
            "            \"allmoney\": 60,\n" +
            "            \"rq\": \"2016-12\"\n" +
            "        }\n" +
            "    },\n" +
            "    \"teams_zprice\": \"24474.00\"\n" +
            "}";
    ArrayList<Goods> goodses = new ArrayList<Goods>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        JSONParser jp=new JSONParser();
        goodses= (ArrayList<Goods>) jp.getJSONParserResult(json,"list");
      //  Log.e("onCreate: ",goodses.get(1).goods.allmoney+"" );
    }
    public static class JSONParser {
        public List<Goods> getJSONParserResult(String JSONString, String key) {
            List<Goods> list = new ArrayList<Goods>();
            JsonObject result = new JsonParser().parse(JSONString)
                    .getAsJsonObject().getAsJsonObject(key);得到list的对象
            Iterator<?> iterator = result.entrySet().iterator();
            while (iterator.hasNext()) {遍历list对象的数据
                @SuppressWarnings("unchecked")
                Map.Entry<String, JsonElement> entry = (Map.Entry<String, JsonElement>) iterator
                        .next();
                Goods cityinfo = new Goods();
                Goods.BBean bb=new Goods.BBean();
                cityinfo.setid(entry.getKey());//key 
                JsonObject object=entry.getValue().getAsJsonObject();//value为对象
                bb.setAllmoney(object.get("allmoney").getAsDouble());
                bb.glj_price= object.get("glj_price").getAsDouble() ;
                bb.lrj_price= object.get("lrj_price").getAsDouble() ;
                bb.fdj_price= object.get("fdj_price").getAsDouble() ;
                bb.rq= object.get("rq").getAsString() ;
                cityinfo.setgoods(bb);
                Log.e("getJSONParserResult: ",cityinfo.getgoods().getAllmoney()+"元"+ cityinfo.getid());
                list.add(cityinfo);
            }
            return list;
        }
    }
}
//ok
望指正!!!!!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值