Gson 解析 json 为javabean
来源:互联网 发布:mac qq不能接收文件夹 编辑:程序博客网 时间:2024/05/17 04:50
解析的代码:
package com.justsy.test;
import java.util.List;
import com.google.gson.Gson;
public class TestGson {
public static void main(String[] args) {
String gs = "{"+
"country:{"+
"city:{"+
"persons:["+
"{name:gaofeng,age:22},"+
"{name:bing,age:23}"+
"]"+
"}"+
"}"+
"}";
Gson gson = new Gson();
T t = gson.fromJson(gs, T.class);
List<Person> persons = t.getCountry().getCity().getPersons();
System.out.println("c:"+persons);
for(Person p:persons){
System.out.println("pname:"+p.getName());
System.out.println("page:"+p.getAge());
}
}
class T{
private Country country;
public T(){}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
}
class Country{
private City city;
public Country(){}
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
}
class City{
private List<Person> persons;
public City(){}
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
}
class Person{
private String name;
private int age;
public Person(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}
拼字符串拼的我好辛苦......
package com.justsy.test;
import java.util.List;
import com.google.gson.Gson;
public class TestGson {
public static void main(String[] args) {
String gs = "{"+
"country:{"+
"city:{"+
"persons:["+
"{name:gaofeng,age:22},"+
"{name:bing,age:23}"+
"]"+
"}"+
"}"+
"}";
Gson gson = new Gson();
T t = gson.fromJson(gs, T.class);
List<Person> persons = t.getCountry().getCity().getPersons();
System.out.println("c:"+persons);
for(Person p:persons){
System.out.println("pname:"+p.getName());
System.out.println("page:"+p.getAge());
}
}
class T{
private Country country;
public T(){}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
}
class Country{
private City city;
public Country(){}
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
}
class City{
private List<Person> persons;
public City(){}
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
}
class Person{
private String name;
private int age;
public Person(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}
拼字符串拼的我好辛苦......
0 0
- Gson 解析 json 为javabean
- javabean问题,gson解析
- Gson 解析 json中 value为数组
- gson解析json数据格式为object对象
- 利用Gson将Json 转化为Map,javaBean, List转换为json
- JSON解析 GSON解析
- Android解析——json解析为javabean
- json解析:gson解析json
- Android 使用GSON解析JSON 并转换为JAVA对象
- 使用Gson解析键名为中文的json字符串
- JAVA将String转换为json并用gson解析
- 使用Gson解析Json为Map对象探索(上)
- 使用Gson解析Json为Map对象探索(下)
- Gson解析Json数据
- 使用Gson解析Json
- Android JSON Gson 解析
- 使用Gson解析json
- Gson解析json
- JAVA修饰符类型
- Sicily 2010. H Number
- popupWindow从下往上弹出效果的实现
- IT从业者的职业道路(从程序员到部门经理)
- 带加载进度条的WebView
- Gson 解析 json 为javabean
- css改变下拉列表select框的默认样式
- cookie中存、取json格式的数据
- mysql的学习笔记
- Android 深入探讨sigleTask和singletop的区别
- 【OpenCV入门指南】第四篇 图像的二值化
- [leetcode] 17. Letter Combinations of a Phone Number
- PowerBuilder命令行编译详解[1]
- MySQL创建用户与授权方法