簡體   English   中英

在Android中解析嵌套的JSON對象

[英]Parsing nested JSON object in Android

我正在嘗試解析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);`

但是,當我嘗試使用“名稱:”時,它將無法正常工作。

我試過了:

JSONObject businessObject = jObject.getJSONObject("business");
String nameValue = businesObject.getString("name");
System.out.println(nameValue);

當我嘗試這樣做時,我得到“找不到JSONObject [業務]”。

當我嘗試:

String nameValue = offerObject.getString("name");
System.out.println(nameValue);`

我得到了預期的“找不到JSONObject [名稱]”。

我在這里做錯了什么? 我缺少一些基本的東西。

好吧,我是個白痴。 這可行。

JSONObject businessObject = offerObject.getJSONObject("business");
String nameValue = businessObject.getString("name");
System.out.println(nameValue);

如果我只想想兩秒鍾,然后再發布...哎呀!

這里是單行解決方案

String myString = myJsonObject.getJSONObject("offer").getJSONObject("business").getString("name");

請注意,不必“手動”對Java對象進行JSON序列化/反序列化。 GSONJackson 這類的圖書館都非常容易。

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策略不適用於JSON屬性名稱(例如“ address_1”)。

如果需要考慮JSON處理的性能,請查看JacksonVs。 Gsonhttp://www.cowtowncoder.com/blog/archives/2011/01/entry_437.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM