json在線html
使用Gson輕鬆解決複雜結構的Json數據解析 基礎比較好java
2016大學裏的流年回憶json
xudq: 內部靜態類 GSON反序列:數組
class TestBean //implements Serializable
{
String title = "okyala";
boolean isShow = true;//這裏給isShow設置了默認值,坑點spa
// 只須要聲明爲靜態的就能夠保持初值
static public class InnerBean
{
int width = 10;
int height = 40;
}
public InnerBean inner_bean = new InnerBean();
}.net
TestBean testbean = gson.fromJson("{title:\"標題\", isShow:\"false\"}", TestBean.class);
MyLog.logMsg("testbean title:" + testbean.title + " isShow:" + testbean.isShow + " width:"+ testbean.inner_bean.width + " height:" + testbean.inner_bean.height);code
java.lang.reflect.Type type = new TypeToken<TestBean>() {}.getType();
testbean = gson.fromJson("{title:\"標題\", inner_bean:{width:5}}", type);
MyLog.logMsg("2testbean title:" + testbean.title + " isShow:" + testbean.isShow + " width:"+ testbean.inner_bean.width + " height:" + testbean.inner_bean.height);orm
11-24 11:43:38.483 20912-20912/com.tz E/com.tz: testbean title:標題 isShow:false width:10 height:40
11-24 11:43:38.484 20912-20912/com.tz E/com.tz: 2testbean title:標題 isShow:true width:5 height:40htm
Android實戰——GsonFormat、Gson、fastjson快速搞定JSON數據解析對象
序列化:將對象轉換成json
簡單重構
爲POJO字段提供備選屬性名SerializedName
註解提供了兩個屬性,上面用到了其中一個,別外還有一個屬性alternate
,接收一個String數組。
注:alternate
須要2.4版本
@SerializedName(value = "emailAddress", alternate = {"email", "email_address"}) public String emailAddress;
iOS json 解析遇到error: Error Domain=NSCocoaErrorDomain Code=3840
oc 字符串轉json
[NSJSONSerialization JSONObjectWithData:dd options:NSJSONReadingMutableLeaves error:&error];