如何将JSON元素标签设置为ListView? [英] How to set JSON element tag into ListView?
问题描述
我有这个JSON文件,我必须通过单击以在WebView中打开url值,将标记"info"的每个值放入ListView中.使用"title" JSON标签的值设置ActionBar标题.
I have this JSON file and I must put each value of tag "info" into a ListView with click to open the url value in a WebView. The ActionBar title is set with value of the "title" JSON tag.
{
"mobiledata": {
"geoJson_1": "http://###.###.###",
"geoJson_2": "",
"info": [
{
"title": "Italia",
"uri": "https://mysite.wordpress.com/i/"
},
{
"title": "Il tempo",
"uri": "https://mysite.wordpress.com/il/"
},
{
"title": "Le info",
"uri": "https://mysite.wordpress.com/la-terra/"
},
{
"title": "Il rischio",
"uri": "https://mysite.wordpress.com/italia/regioni-italiane/"
},
{
"title": "Le Rubriche mensili",
"uri": "https://mysite.wordpress.com/tag/rubricamensile/"
}
]
}
}
我该怎么做?有帮助或示例吗?
How can I do this? Any help or examples?
推荐答案
更新
旧答案中给出的方法太冗长.改善步骤:
Update
Approach given in old answer is too verbose. Steps to improve:
-
使用Gson库,它既快速又紧凑.添加到您的模块级build.gradle文件中:
Use Gson library, it's fast and compact. Add into your module-level build.gradle file:
dependencies {
// your dependencies: AppCompat, HTTP client etc
compile 'com.google.code.gson:gson:2.7'
}
声明适当的数据结构:
Declare appropriate data structure:
class InfoItem {
String title;
String uri;
}
解析数据:
Parse data:
JsonElement data = new JsonParser().parse(json);
获取mobiledata.info:
Get mobiledata.info:
JsonArray info = data
.getAsJsonObject() // treat parsed data as map
.getAsJsonObject("mobiledata") // get 'mobiledata' as map
.getAsJsonArray("info"); // get 'info' as list
将已解析的jsonObject映射到POJO
map parsed jsonObjects into POJOs
List<InfoItem> items = new Gson()
.fromJson(info, new TypeToken<List<InfoItem>>(){}.getType());
使用您自己的适配器.使用ViewHolder
时,模式绑定将如下所示:
Use your own adapter. With ViewHolder
pattern binding will look like this:
InfoItem item = items.get(position);
holder.title.setText(item.title);
holder.uri.setText(item.uri);
P. S.您可能会缓存此丑陋的TypeToken:
P. S. You may cache this ugly TypeToken:
private static final Type type =
new TypeToken<List<InfoItem>>(){}.getType();
要测试的完整代码:
public class SoJsonAnswer {
private static final Type type =
new TypeToken<List<InfoItem>>(){}.getType();
public static void main(String[] args) {
String json = "{paste it here}";
JsonElement data = new JsonParser().parse(json);
JsonArray info = data
.getAsJsonObject() // treat parsed data as map
.getAsJsonObject("mobiledata") // get map
.getAsJsonArray("info"); // get list
// map parsed jsonObjects into POJOs
List<InfoItem> items = new Gson().fromJson(info, type);
System.out.println(items);
}
}
class InfoItem {
String title;
String uri;
@Override
public String toString() {
return "title=" + title + ", uri=" + uri;
}
}
原始答案
-
在您的项目中包含一个JSON库. 我使用以下代码: https://code.google.com/p/json-simple /downloads/list
Include a JSON library in your project. I use this: https://code.google.com/p/json-simple/downloads/list
解析为对象.
String json = "{...}"; //Your JSON here
JSONParser parser = new JSONParser(); //Can cause ParseException
JSONObject obj = (JSONObject) parser.parse(json); //Can cause ClassCastException
获取内部数据.
Get inner data.
JSONObject mobileData = (JSONObject) obj.get("mobiledata");
JSONArray info = (JSONArray) mobileData.get("info");
将此数据打包到ArrayList中.只需使用for
循环,将get
您的物品投放到String
.
Pack this data into an ArrayList. Just use for
cycle, get
your items and cast them to String
.
ArrayList<Map<String, Object>> data = new ArrayList<>(info.size());
Map<String, Object> m;
JSONObject current;
for (int i = 0; i < info.size(); i++) {
m = new HashMap<String, Object>();
current = (JSONObject) info.get(i);
m.put("title", (String) current.get("title"));
m.put("url", (String) current.get("url"));
data.add(m);
}
为ListView
设置Adapter
.
String[] from = { "title", "url" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2,
from, to);
lvList.setAdapter(adapter);
希望它会有所帮助. 代码未经测试.
Hope it helps. Code not tested.
这篇关于如何将JSON元素标签设置为ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!