在Android中解析嵌套的JSON对象
我正在尝试解析一个 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] not found”。
当我尝试时:
String nameValue = offerObject.getString("name");
System.out.println(nameValue);`
正如预期的那样,我得到“未找到 JSONObject [name]”。
我在这里做错了什么?我缺少一些基本的东西......
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% 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"
}
},
And so on....
So far, I'm able to parse very simply, by doing this kinda thing:
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);`
But when I try it for 'name:' it won't work.
I've tried:
JSONObject businessObject = jObject.getJSONObject("business");
String nameValue = businesObject.getString("name");
System.out.println(nameValue);
When I try that, I get "JSONObject [business] not found."
And when I try:
String nameValue = offerObject.getString("name");
System.out.println(nameValue);`
I get, as expected, "JSONObject [name] not found".
What am I doing wrong here? I'm missing something basic....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我是个白痴。这有效。
如果我在发帖前想两秒钟就好了……天啊!
Ok, I'm an idiot. This works.
If I would only think for two seconds before posting... Jees!
这是单行解决方案
Here a single-line solution
请注意,与 Java 对象之间的 JSON 序列化/反序列化不必“手动”完成。 GSON 和 Jackson 使这变得非常容易。
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".
如果您关心 JSON 处理的性能,请查看 Jackson Vs. Gson 和 http://www.cowtowncoder.com/博客/archives/2011/01/entry_437.html
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.
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