Android中解析不规则json的理解(基于Gson)
来源:互联网 发布:知乎问题 编辑:程序博客网 时间:2024/06/10 17:31
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"
但是有一个细节 是 list 中 项数目不定 而且 每一个 项对应一个object
+++++++++++++++++++++++++++++++++
下面是我在百度的基础上改的 注意 里面所有的jsonobject 均为gson包下的 不是原生的 原生应该也能写
==================================
望指正!!!!!!!
"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 0
- Android中解析不规则json的理解(基于Gson)
- Android中Gson解析json
- Android中解析json数据的方式之一:Gson
- Android中使用Gson解析JSON数据的方法
- Android中使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- Android 中 使用 Gson解析json
- Android中使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- Android中使用gson解析JSON
- Android中Gson解析详解json
- Android中使用Gson解析JSON数据
- Android 中 使用 Gson解析json
- Android中使用Gson解析JSON数据
- Android中利用Gson解析Json
- Android中使用Gson解析JSON数据
- 打印1到最大的n位数
- 深度学习统计学基础
- 【PAT】【Advanced Level】1069. The Black Hole of Numbers (20)
- 谷歌正式发布Android 8.0,TestBird支持Android Oreo测试
- JAVA开发的23种设计模式之 --- 状态模式
- Android中解析不规则json的理解(基于Gson)
- navigationview的样式设置
- 记录一下关于编码的问题
- SequoiaDB巨杉数据库大对象块存储原理与应用
- PAT甲级1002. A+B for Polynomials (25)
- maven学习笔记:命令行模式创建scala_spark项目并运行程序
- iframe 高度自适应,并且子页面的高度不断变化
- Java处理csv文件
- F: 火柴棒等式