在 Android 中解析嵌套的 JSON 对象
Posted
技术标签:
【中文标题】在 Android 中解析嵌套的 JSON 对象【英文标题】:Parsing nested JSON object in Android 【发布时间】:2011-08-24 13:47:15 【问题描述】:我正在尝试解析一个 JSON 对象,其中一部分如下所示:
"offer":
"category":"Salon",
"description":"Use this offer now to enjoy this great Salon at a 20% discount. ",
"discount":"20",
"expiration":"2011-04-08T02:30:00Z",
"published":"2011-04-07T12:00:33Z",
"rescinded_at":null,
"title":"20% off at Jun Hair Salon",
"valid_from":"2011-04-07T12:00:31Z",
"valid_to":"2011-04-08T02:00:00Z",
"id":"JUN_HAIR_1302177631",
"business":
"name":"Jun Hair Salon",
"phone":"2126192989",
"address":
"address_1":"12 Mott St",
"address_2":null,
"city":"New York",
"cross_streets":"Chatham Sq & Worth St",
"state":"NY",
"zip":"10013"
,
等等……
到目前为止,我可以通过做这样的事情来非常简单地解析:
JSONObject jObject = new JSONObject(content);
JSONObject offerObject = jObject.getJSONObject("offer");
String attributeId = offerObject.getString("category");
System.out.println(attributeId);
String attributeValue = offerObject.getString("description");
System.out.println(attributeValue);
String titleValue = offerObject.getString("title");
System.out.println(titleValue);`
但是当我尝试使用 'name:' 时,它不起作用。
我试过了:
JSONObject businessObject = jObject.getJSONObject("business");
String nameValue = businesObject.getString("name");
System.out.println(nameValue);
当我尝试这样做时,我得到“未找到 JSONObject [business]”。
当我尝试时:
String nameValue = offerObject.getString("name");
System.out.println(nameValue);`
正如预期的那样,我得到“未找到 JSONObject [name]”。
我在这里做错了什么?我缺少一些基本的东西......
【问题讨论】:
嗨,我刷新了以下页面并得到了解决方案***.com/questions/5986123/… 【参考方案1】:这里是单行解决方案
String myString = myJsonObject.getJSONObject("offer").getJSONObject("business").getString("name");
【讨论】:
【参考方案2】:请注意,将 JSON 序列化/反序列化到 Java 对象/从 Java 对象序列化/反序列化不必“手动”完成。 GSON 和 Jackson 之类的库让这一切变得非常简单。
import java.text.DateFormat;
import java.util.Date;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class Foo
static String jsonInput =
"" +
"\"offer\":" +
"\"category\":\"Salon\"," +
"\"description\":\"Use this offer now to enjoy this great Salon at a 20% discount. \"," +
"\"discount\":\"20\"," +
"\"expiration\":\"2011-04-08T02:30:00Z\"," +
"\"published\":\"2011-04-07T12:00:33Z\"," +
"\"rescinded_at\":null," +
"\"title\":\"20% off at Jun Hair Salon\"," +
"\"valid_from\":\"2011-04-07T12:00:31Z\"," +
"\"valid_to\":\"2011-04-08T02:00:00Z\"," +
"\"id\":\"JUN_HAIR_1302177631\"," +
"\"business\":" +
"\"name\":\"Jun Hair Salon\"," +
"\"phone\":\"2126192989\"," +
"\"address\":" +
"\"address_1\":\"12 Mott St\"," +
"\"address_2\":null," +
"\"city\":\"New York\"," +
"\"cross_streets\":\"Chatham Sq & Worth St\"," +
"\"state\":\"NY\"," +
"\"zip\":\"10013\"" +
"" +
"" +
"" +
"";
public static void main(String[] args) throws Exception
GsonBuilder gsonBuilder = new GsonBuilder();
// gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
gsonBuilder.setDateFormat(DateFormat.LONG);
Gson gson = gsonBuilder.create();
OfferContainer offerContainer = gson.fromJson(jsonInput, OfferContainer.class);
System.out.println(offerContainer);
class OfferContainer
private Offer offer;
@Override
public String toString()
return offer.toString();
class Offer
private Category category;
private String description;
private String discount;
private Date expiration;
private Date published;
private String rescinded_at;
private String title;
private Date valid_from;
private Date valid_to;
private String id;
private Business business;
@Override
public String toString()
return String.format(
"[Offer: category=%1$s, description=%2$s, discount=%3$s, expiration=%4$s, published=%5$s, rescinded_at=%6$s, title=%7$s, valid_from=%8$s, valid_to=%9$s, id=%10$s, business=%11$s]",
category, description, discount, expiration, published, rescinded_at, title, valid_from, valid_to, id,
business);
enum Category
Salon
class Business
private String name;
private String phone;
private Address address;
@Override
public String toString()
return String.format(
"[Business: name=%1$s, phone=%2$s, address=%3$s]",
name, phone, address);
class Address
private String address_1;
private String address_2;
private String city;
private String cross_streets;
private String state;
private String zip;
@Override
public String toString()
return String.format(
"[Address: address_1=%1$s, address_2=%2$s, city=%3$s, cross_streets=%4$s, state=%5$s, zip=%6$s]",
address_1, address_2, city, cross_streets, state, zip);
请注意,可以使用 FieldNamingPolicy 轻松地将属性名称从 JSON 映射到 Java 代码。不幸的是,LOWER_CASE_WITH_UNDERSCORES 策略不适用于“address_1”等 JSON 属性名称。
如果关注 JSON 处理的性能,请查看 Jackson Vs. Gson 和 http://www.cowtowncoder.com/blog/archives/2011/01/entry_437.html
【讨论】:
哇,这东西真棒。非常感谢。本来会接受我自己的答案,但现在我不太确定.... ;) 我忘了提几件事。 1. GSON 内置于 android 3.0+。只需使用 AndroidJsonFactory。 2. HTC 人员介绍了在某些 HTC 设备上使用 GSON 的错误。解决方法是只使用 jarjar 将 GSON 代码移动到另一个包。见code.google.com/p/google-gson/issues/detail?id=255 是一个单独的问题,但是从 HttpURLConnection 获取 json 字符串的最简单方法是什么? @Programmer Bruce - 我如何将 json 数组放入我的 String.format 方法中? 我不知道那是什么意思。您是否尝试序列化 Java 对象以生成 JSON 字符串?【参考方案3】:好吧,我是个白痴。这行得通。
JSONObject businessObject = offerObject.getJSONObject("business");
String nameValue = businessObject.getString("name");
System.out.println(nameValue);
如果我在发帖前只想两秒钟……天哪!
【讨论】:
哈哈,很高兴你知道了!不要忘记接受你自己的答案,我认为有某种虚荣徽章;-)以上是关于在 Android 中解析嵌套的 JSON 对象的主要内容,如果未能解决你的问题,请参考以下文章
android使用gson解析嵌套复杂的json数据,数据怎么显示到布局上,布局怎么写
Android Retrofit 数组列表中的嵌套 JSON 对象