Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在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);`

但是,当我尝试’名称’时,它将无法正常工作.

我试过了

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]”.

在这做错了什么?我遗漏了一些基本的东西……

解决方法

好吧,我是个白痴.这很有效.
JSONObject businessObject = offerObject.getJSONObject("business");
String nameValue = businessObject.getString("name");
System.out.println(namevalue);

如果我只想在张贴前两秒钟……杰斯!

大佬总结

以上是大佬教程为你收集整理的在Android中解析嵌套的JSON对象全部内容,希望文章能够帮你解决在Android中解析嵌套的JSON对象所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。