简体   繁体   English

使用JSON解析RSS Feed并填充列表Android应用

[英]Parsing RSS Feed with JSON and Populating a List Android App

Im brand new to Android Application development and I am working on an application that will get an RSS feed from a specific url that returns JSON data , what I am wondering is what is the best way to translate this from JSON to populate the list , 我是Android应用程序开发的新手,我正在开发一个应用程序,该应用程序将从返回JSON数据的特定URL获取RSS提要,我想知道什么是从JSON转换为列表的最佳方法,

I was thinking of making objects to hold the individual posts and then create a list of them from the json but is this the best way it seems a little rough 我当时正在考虑制作对象以保留各个帖子,然后从json创建它们的列表,但这是最好的方法,似乎有些粗糙

Just looking for idea on how others perform this action May also be helpful to other beginners as no concrete reference base is around for this subject. 只是寻找关于其他人如何执行此操作的想法可能也对其他初学者有所帮助,因为该主题没有具体的参考依据。

thanks chris 谢谢克里斯

Very basic example: 很基本的例子:

String resultString = "{"name":"Fred Nurke", "age":"56"}";
JSONObject jobj = new JSONObject(resultString); /*This converts the string 
                                                  into members of the JSON Object which   
                                                  you can then manipulate*/
Log.d(jobj.getString("name")+ " is " + jobj.getString("age") + " years old");

As I said that is a very basic example of working with JSON in Android. 正如我所说的,这是在Android中使用JSON的非常基本的示例。 What you'll probably be dealing with is not just a JSONObject, but a JSONArray, which as the name implies is a special Array class of JSONObjects. 您可能要处理的不仅是一个JSONObject,而且是一个JSONArray,顾名思义,这是一个特殊的JSONObjects Array类。

Working with a JSONArray can be done as follows: (Assume we've filled the resultString already) 可以使用JSONArray进行以下操作:(假设我们已经填充了resultString)

JSONArray jsonArr = new JSONArray(resultString);
JSONObject jsonobj;
for(int i = 0; i < jsonArr.length(); i++){
    jsonobj = JSONArray.getJSONObject(i);
}

Once you have your JSONObject you can start working with it in the same manner as above. 拥有JSONObject之后,就可以按照上述相同的方式开始使用它。

Hope that helps. 希望能有所帮助。

The android API has built in support for handling JSON data which is helpful for parsing. android API内置了对处理JSON数据的支持,这有助于解析。 It uses the classes shown here: http://www.json.org/java/ 它使用此处显示的类: http : //www.json.org/java/

API reference from android here: http://developer.android.com/reference/org/json/JSONObject.html 来自android的API参考: http : //developer.android.com/reference/org/json/JSONObject.html

At a high level, You can create a JSONObject by simply passing the entire JSON string into its constructor. 在较高的层次上,您可以通过简单地将整个JSON字符串传递到其构造函数中来创建JSONObject。 From there it offers a list of get methods to pull primitive types, strings, or nested JSON objects out. 从那里,它提供了get方法的列表,以拉出基本类型,字符串或嵌套的JSON对象。

If you want to keep the posts saved, I would still parse the JSON data into an object for storage, otherwise you could just create a list from the data returned directly from the JSON objects. 如果您想保留帖子,则我仍将JSON数据解析为一个对象进行存储,否则,您可以根据直接从JSON对象返回的数据创建一个列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM