gpt4 book ai didi

java - "no value for rss",虽然它显然在那里

转载 作者:行者123 更新时间:2023-11-30 02:48:44 25 4
gpt4 key购买 nike

我正在尝试在 Android 项目中读取来自 CNN 的 RSS 提要。到目前为止一切正常。我成功建立了连接并将整个 XML 文件检索为一个字符串。然后我尝试创建一个 JSON 对象并解析它。但是它的某些部分无法读取。我试图读取的 XML 是这个

查看源代码:http://rss.cnn.com/rss/edition_world.rss

为了简化并使一切更加清晰,我附上了一张在 JSON 对象编辑器中查看的 RSS 提要的图片:

http://tinypic.com/view.php?pic=2iiaezb&s=8#.U7ax0vmGFmw

所以代码是这样的,

//It successfully converts the text to JSON
JSONObject jObj = new JSONObject(responseText);

String respTime = jObj.getString("responseTime");
//It successfully prints the responseTime
System.out.println("Response time is: " + respTime);

JSONObject respHeader = jObj.getJSONObject("responseHeaders");
String date = respHeader.getString("date");
//It successfully prints the date as well
System.out.println("Date is: "+ date);


//However it says no value for rss found
JSONObject rssObj = jObj.getJSONObject("rss");
JSONObject channelObj = rssObj.getJSONObject("channel");
JSONArray itemArr = channelObj.getJSONArray("item");

“responseTime”、“responseHeaders”和“rss”在 XML 文件的层次结构方面都是等效的,正如您从我引用的图像中看到的那样。因此,虽然我能够读取“responseTime”和“responseHeaders”,但为什么它说找不到“rss”的值,因此我无法访问它的任何子项?

最佳答案

我找到了答案。实际的 JSON 对象字符串,即 responseText,与 XML 文件中出现的内容不同。这是不同的东西,我花了一些时间才意识到这一点。我无法从 Eclipse 的 logcat 中复制它,因为它的缓冲区有限,所以我不得不通过写入文本文件来研究它。然后我可以正确解析它。

关于java - "no value for rss",虽然它显然在那里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24576081/

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