gpt4 book ai didi

android - 在 Android 中解析嵌套的 JSON 对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:05:46 26 4
gpt4 key购买 nike

我正在尝试解析一个 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 [业务]”。

当我尝试时:

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

如预期的那样,我收到“未找到 JSONObject [名称]”。

我在这里做错了什么?我缺少一些基本的东西....

最佳答案

好吧,我是个白痴。这行得通。

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

要是我在发帖前想两秒钟就好了……哎呀!

关于android - 在 Android 中解析嵌套的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5986123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com