我正在尝试解析一个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 [business]。”
当我尝试时:
String nameValue = offerObject.getString("name");
System.out.println(nameValue);`
我按预期得到“找不到JSONObject [name]”。
我在这里做错了什么?我错过了一些基本的东西......
答案 0 :(得分:31)
JSONObject businessObject = offerObject.getJSONObject("business");
String nameValue = businessObject.getString("name");
System.out.println(nameValue);
如果我在张贴之前只考虑两秒钟......杰斯!
答案 1 :(得分:14)
这是一个单行解决方案
String myString = myJsonObject.getJSONObject("offer").getJSONObject("business").getString("name");
答案 2 :(得分:3)
请注意,不必“手动”完成向/从Java对象序列化/反序列化JSON。像GSON和Jackson这样的图书馆非常容易。
请注意,FieldNamingPolicy可用于轻松地将属性名称从JSON映射到Java代码。遗憾的是,LOWER_CASE_WITH_UNDERSCORES策略不适用于“address_1”等JSON属性名称。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); } }
如果需要考虑JSON处理的性能,请查看Jackson Vs. Gson和http://www.cowtowncoder.com/blog/archives/2011/01/entry_437.html