天天看點

com.google.gson string to JsonObject

gson 中字元串轉換為json資料:

String testString="'bgColorPc ':'red'";

JsonObject jsondetail = new JsonParser().parse(testString).getAsJsonObject();

String bgColorPc = jsondetail.get("bgColorPc").getAsString();

這樣就得到了jsonObject對象了,并且得到了jsonObject對象的值

方法二;

JsonElement jelem=gson.fromJson(json,JsonElement.class);

JsonObject jobj=jelem.getAsJsonObject();

gson 指南

https://sites.google.com/site/gson/gson-user-guide#TOC-Primitives-Examples

gson中字元串轉化為固定對象

String colorTotal=“”;

JifenBgColorExtendDo bgColor=new Gson().fromJson(colorTotal, JifenBgColorExtendDo.class);

這樣就可以将字元串轉化為對象了

不過字元串中的key值要與對象的值一一對應