解析Android中嵌套的JSON对象 [英] Parsing nested JSON object in Android
本文介绍了解析Android中嵌套的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图解析一个JSON对象,其中有一部分是这样的:
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.
我已经试过:
JSONObject businessObject = jObject.getJSONObject("business");
String nameValue = businesObject.getString("name");
System.out.println(nameValue);
当我尝试了,我得到的JSONObject [商家]没有找到。
When I try that, I get "JSONObject [business] not found."
当我尝试:
String nameValue = offerObject.getString("name");
System.out.println(nameValue);`
我得到,正如所料,JSONObject的[名]未找到。
I get, as expected, "JSONObject [name] not found".
我在做什么错在这里?我失去了一些东西基本....
What am I doing wrong here? I'm missing something basic....
推荐答案
好吧,我是个白痴。这个工程。
Ok, I'm an idiot. This works.
JSONObject businessObject = offerObject.getJSONObject("business");
String nameValue = businessObject.getString("name");
System.out.println(nameValue);
如果我只觉得两秒钟,然后再发布... JEES!
If I would only think for two seconds before posting... Jees!
这篇关于解析Android中嵌套的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文