android studio解析JSON数据

xiaoxiao2021-02-28  119

第一步,gradle添加:compile 'com.google.code.gson:gson:2.2.4' 数据源为:oringdata:{"status":0,"size":0,"total":0,"page":0,"cur":0,"data":{"value":26.0,"id":"5","tiid":5}}时: Gson gson = new Gson(); String response = orgindata; JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject(); JsonObject data = jsonObject.get("data").getAsJsonObject(); Humi humi= gson.fromJson(data,Humi.class);数据源为:oringdata{"status":0,"size":5,"total":0,"page":0,"cur":0,"data":[{"tempdesc":"****","value":25.0,"id":"1","tiid":1},{"value":25.0,"id":"2","tiid":2},{"value":25.6,"id":"3","tiid":3},{"value":25.6,"id":"4","tiid":4},{"value":26.0,"id":"5","tiid":5}]}时: Gson gson = new Gson(); String response = origindata;JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject(); JsonArray array = jsonObject.get("data").getAsJsonArray(); for(JsonElement type : array){ Humi humi = gson.fromJson(type, Humi.class); L.E(humi.toString()); }
转载请注明原文地址: https://www.6miu.com/read-47545.html

最新回复(0)