Gson解析json資料

2021-09-17 21:08:46 字數 1319 閱讀 4301

使用gson解析json資料

1.當json資料是jsonobject時對應大括號

2.當json資料是jsonarray時對應中括號

[person [name=xiaoming, age=1, address=meiguo, colorstring= null, height =0], person [name=xiaoming1, age=2, address=meiguo1, colorstring= null, height =0], person [name=xiaoming2, age=3, address=meiguo2, colorstring= null, height =0]]

[,,]

/**

* @param args

* 實體類中增加屬性,而json資料中沒有該欄位,不影響生成實體類,則生成的實體json資料不包含該欄位部分為預設值

* 使用gson gson = new gsonbuilder().serializenulls().create();在json資料的字段值值為空的時候也保留該字串

*/public static void main(string args) ";

string arraystring ="," +

"," +

"]}";

string arraystrings ="[," +

"," +

"]";

testjsontoobject(jsonstring);

testobjecttojson(arraystring);

tokenobjecttojson(arraystrings);

}public static void testjsontoobject(string jsonstring)

public static void testobjecttojson(string json)

//使用typetoken解析成集合

public static void tokenobjecttojson(string json) .gettype();

object fromjson = gson.fromjson(json,type);

system.out.println(fromjson);

string json2 = gson.tojson(fromjson);//生成json資料

system.out.println(json2);

}} class bean2

}@override

public string tostring()

}

Gson解析json資料

1 解析單個物件 json字串如下 封裝解析的物件 public class nbamatchs public void setformatdate string formatdate public string getdate public void setdate string date pub...

json資料解析(gson)

string json type type new typetoken gettype suppresswarnings unchecked mapdata map new gson fromjson json,type iterator iterator data.entryset iterato...

Gson解析json資料

1 解析單個物件 json字串如下 封裝解析的物件 public class nbamatchs public void setformatdate string formatdate public string getdate public void setdate string date pub...