Parsing Nested Json Object In Android
I'm trying to parse a JSON object, part of which looks like this: { 'offer':{ 'category':'Salon', 'description':'Use this offer now to enjoy this great Salon at a 20% disco
Solution 1:
Ok, I'm an idiot. This works.
JSONObject businessObject = offerObject.getJSONObject("business");
String nameValue = businessObject.getString("name");
System.out.println(nameValue);
If I would only think for two seconds before posting... Jees!
Solution 2:
Here a single-line solution
StringmyString= myJsonObject.getJSONObject("offer").getJSONObject("business").getString("name");
Solution 3:
Note that serializing/deserializing JSON to/from Java objects doesn't have to be done "manually". Libraries like GSON and Jackson make it very easy.
import java.text.DateFormat;
import java.util.Date;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
publicclassFoo
{
staticString 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\""+"}"+"}"+"}"+"}";
publicstatic void main(String[] args) throwsException
{
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);
}
}
classOfferContainer
{
privateOffer offer;
@OverridepublicString toString()
{
return offer.toString();
}
}
classOffer
{
privateCategory category;
privateString description;
privateString discount;
privateDate expiration;
privateDate published;
privateString rescinded_at;
privateString title;
privateDate valid_from;
privateDate valid_to;
privateString id;
privateBusiness business;
@OverridepublicString toString()
{
returnString.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);
}
}
enumCategory
{
Salon
}
classBusiness
{
privateString name;
privateString phone;
privateAddress address;
@OverridepublicString toString()
{
returnString.format(
"[Business: name=%1$s, phone=%2$s, address=%3$s]",
name, phone, address);
}
}
classAddress
{
privateString address_1;
privateString address_2;
privateString city;
privateString cross_streets;
privateString state;
privateString zip;
@OverridepublicString toString()
{
returnString.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);
}
}
Note that a FieldNamingPolicy can be used to easily map attribute names from the JSON to the Java code. The LOWER_CASE_WITH_UNDERSCORES policy unfortunately does not work with JSON attribute names like "address_1".
If the performance of JSON handling is a concern, then take a look at Jackson Vs. Gson and http://www.cowtowncoder.com/blog/archives/2011/01/entry_437.html
Post a Comment for "Parsing Nested Json Object In Android"