Gson 解析json 字符串的问题

gaofeng2009123 2012-07-26 02:54:16
Gson 解析json 我只会两种,一种是: {"name":"gaofeng","age":"20"}
还有一种就是数组: [{"name":"gaofeng","age":"20"},{"name":"myname","age":"22"}]

可是现在我碰见了一种这样的json格式就不会解析了,请各位帮帮小弟吧,下面的怎么解析呢?
{
"country":{

"city":{
"persions":[
{"name":"gaofeng","age":"22"},
{"name":"wangwu","age":"20"},
{"name":"lisi","age":"22"},
]
}
}
}

网上说可以这样解析:
class A{
public String name;
public String age; //get set 方法就不写了

}


class B{

public String country;
public String city;
public List<A> persions;
}

下面就说解析B对象,可是怎么解析呢?总是报错呀,错误如下
Expected a string but was BEGIN_OBJECT at line 1 column 40。 现在我就想知道怎么解析B呢?拜托各位了,谢谢

...全文
11630 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kevin_Rosen 2014-12-03
  • 打赏
  • 举报
回复
引用 10 楼 rosenluo 的回复:
你country在json数据里是对象表示,你定义B时却用字符串,当然会出错!
还有就是city也是同样的问题!
Kevin_Rosen 2014-12-03
  • 打赏
  • 举报
回复
你country在json数据里是对象表示,你定义B时却用字符串,当然会出错!
wxy476599729 2014-07-03
  • 打赏
  • 举报
回复
引用 1 楼 Tibib 的回复:
解析的代码: 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; } } } 拼字符串拼的我好辛苦......
受教了!!!!!
tianfei70 2014-02-26
  • 打赏
  • 举报
回复
/** * 用来解析获取当前登录用户及其所关注用户的最新微博 * @param jsonString :传入将要解析的String * @return */ public static LinkedList<NewWeiBoInfo> weiboJson(String jsonString){ LinkedList<NewWeiBoInfo> list = null; try { JSONObject jsonObject = new JSONObject(jsonString); String sta = jsonObject.getString("statuses"); java.lang.reflect.Type tt = new TypeToken<LinkedList<NewWeiBoInfo>>() {}.getType(); Gson gson=new Gson(); list=gson.fromJson(sta, tt); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return list; }
Carve_Time 2013-10-14
  • 打赏
  • 举报
回复
引用 1 楼 Tibib 的回复:
解析的代码: 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; } } } 拼字符串拼的我好辛苦......
学习
Carve_Time 2013-10-14
  • 打赏
  • 举报
回复
good
ly348018 2013-06-14
  • 打赏
  • 举报
回复
我也遇到同样的问题,不知道哦楼主解决了没有
Carve_Time 2013-05-24
  • 打赏
  • 举报
回复
jqueryxuexi 2012-07-26
  • 打赏
  • 举报
回复
积分中,请原谅。
plplum 2012-07-26
  • 打赏
  • 举报
回复
这种情况就要用数组:

JSONArray jsonArray = jsonObject1.getJSONArray(\"persons\");

然后后面的你应该会的
pobi 2012-07-26
  • 打赏
  • 举报
回复
解析的代码:

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

}
}

拼字符串拼的我好辛苦......

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧