java 如何解析后台返回的key和value都未知的json
2018-03-25 01:43
656 查看
问题描述:
后台返回一段json对象,key、value都是动态且字段未知!如下所示,labels对象里面的key、value都是动态变化的。
解决方案:
其他已知字段直接写进实体类的属性就行,那段动态的json对象需要使用
请求下来的数据数据,可以这样操作:
JsonElement(com.google.gson.JsonElement)简介:
该类是一个抽象类,代表着json串的某一个元素。这个元素可以是JsonObject , JsonArray, JsonPrimitive JsonNull等。
JsonElement提供了一系列的方法来判断当前的JsonElement类型:
JsonElement也提供了一系列的方法来获取对应类型的JsonElement:
后台返回一段json对象,key、value都是动态且字段未知!如下所示,labels对象里面的key、value都是动态变化的。
{ bads: 0, average: "8.3", totalEvaluates: 6, goodRate: "50%", servedCompanies: "5", goods: 3, mediums: 3, labels: { 安全专业性强: 6, 建议可行: 4, 对企业有帮助: 3, 服务态度好: 1 } }
解决方案:
其他已知字段直接写进实体类的属性就行,那段动态的json对象需要使用
JsonElement解析。JsonElement可以解析各种json格式。所以上述json可以写入下面的实体类:
public class CreditBean{ /** * bads : 0 * average : 8.0 * totalEvaluates : 2 * goodRate : 50% * servedCompanies : * goods : 1 * mediums : 1 * labels : */ private int bads; private String average; private String totalEvaluates; private String goodRate; private String servedCompanies; private int goods; private int mediums; private JsonElement labels; }
请求下来的数据数据,可以这样操作:
JsonElement labels = credit.getLabels();//获取动态json元素类 Iterator<String> it = json.keys();//使用迭代器 while (it.hasNext()) { String key = it.next();//获取key int value = json.getInt(key);//获取value Log.e("key-value","key="+key+" value="+value); }
JsonElement(com.google.gson.JsonElement)简介:
该类是一个抽象类,代表着json串的某一个元素。这个元素可以是JsonObject , JsonArray, JsonPrimitive JsonNull等。
JsonElement提供了一系列的方法来判断当前的JsonElement类型:
/** * provides check for verifying if this element is an array or not. * * @return true if this element is of type {@link JsonArray}, false otherwise. */ public boolean isJsonArray() { return this instanceof JsonArray; } /** * provides check for verifying if this element is a Json object or not. * * @return true if this element is of type {@link JsonObject}, false otherwise. */ public boolean isJsonObject() { return this instanceof JsonObject; }
JsonElement也提供了一系列的方法来获取对应类型的JsonElement:
/** * convenience method to get this element as a {@link JsonObject}. If the element is of some * other type, a {@link IllegalStateException} will result. Hence it is best to use this method * after ensuring that this element is of the desired type by calling {@link #isJsonObject()} * first. * * @return get this element as a {@link JsonObject}. * @throws IllegalStateException if the element is of another type. */ public JsonObject getAsJsonObject() { if (isJsonObject()) { return (JsonObject) this; } throw new IllegalStateException("Not a JSON Object: " + this); } /** * convenience method to get this element as a {@link JsonArray}. If the element is of some * other type, a {@link IllegalStateException} will result. Hence it is best to use this method * after ensuring that this element is of the desired type by calling {@link #isJsonArray()} * first. * * @return get this element as a {@link JsonArray}. * @throws IllegalStateException if the element is of another type. */ public JsonArray getAsJsonArray() { if (isJsonArray()) { return (JsonArray) this; } throw new IllegalStateException("This is not a JSON Array."); }
相关文章推荐
- JAVA后台接收前台传过来的json字符串并解析获得key 和value
- java 解析json 遍历未知key与value
- JAVA 解析未知JSON并获得key,value,value的类型
- 坑爹后台-json数据key和value未知的解析
- java 解析json 遍历未知key
- Java解析复杂嵌套JSON数据格式代码,提取出来所有的key和value。亲测代码可用
- java 解析 json 遍历未知key
- 递归解析任意层的Json数据,获取所有的key和value,Java实现
- java后台服务器向手机端返回Json, 使用 "net.sf.json.JSONObject" 解析出现的异常问题
- java后台返回json数据前台接收并解析(json)
- 后台返回的json前端如何解析
- java JsonObject的用法以及map总同一个key,但是不同的value,如何存储的问题
- java后台如何获取session的所有内容(获取到key和value的方法)
- rapidjson 实现未知json数据解析,拿到每一个key和value
- [置顶] 动态grid java 后台封装json数据返回到前台解析并展示
- Android 解析后台返回为Json数据的简单例子!!!
- 利用ASIHTTPRequest请求java服务和JSONKit解析返回的json串
- java如何解析json字符串(List<Map<String,Object>成功的类型)
- 如何把Ext.data.store里的数据一次性用JSON传给后台(添加了后台解析部分)
- 后台接收JSON字符串,java解析