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];