解析格式地图&LT的JSON;弦乐,ArrayList的&LT;串GT;&GT;在android系统 [英] Parsing JSON of format Map<String, ArrayList<String>> in android
问题描述
我有JSON对象:
{1:[1,的test.txt,authortest,sizetest],2:[2,GraphTheory.pdf,测试,0 ]}
{"1":["1","test.txt","authortest","sizetest"],"2":["2","GraphTheory.pdf","testing","0"]}
有地图>列表地图;
现在我需要解析它在这里:
Now I need to parse it in here :
给出答案解析像这样做:
As of given answer parsing is done like this :
protected Map<String,ArrayList<String>> doInBackground(
String... arg0) {
JSONParser jParser = new JSONParser();
String json = jParser.getJSONFromUrl(url);
Gson gson = new Gson();
Type type = new TypeToken<Map<String, List<String>>>() {}.getType();
Map<String,ArrayList<String>> allBookList = gson.fromJson(json, type);
return allBookList;
}
下面每个映射包含三个项目的ArrayList:BOOKNAME,BOOKAUTHOR和书本
Here each Mapping contains an arraylist with three items : BOOKNAME,BOOKAUTHOR and BOOKSIZE.
@Override
protected void onPostExecute(Map<String, ArrayList<String>> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(
ViewLibrary.this, result,
R.layout.book_item, new String[] { BOOKNAME,
BOOKAUTHOR,BOOKSIZE }, new int[] { R.id.bookname,
R.id.bookauthor, R.id.booksize });
lv.setAdapter(adapter);
}
现在我需要在一个listview.How显示它做到这一点?
我觉得适配器需要在onPostExecute method.But设置,我无法理解它从哪里得到这三个how.As:BOOKNAME,
BOOKAUTHOR,书本型
Now i need to display it in a listview.How to do this ? I think adapter need to be set in onPostExecute method.But I am not able to understand it how.As from where to get these three : BOOKNAME, BOOKAUTHOR,BOOKSIZE
推荐答案
由于您是新的JSON,你可能需要使用GSON库。要做到这一点在的build.gradle和同步添加上你的依赖
As you are new to JSON you may want to use GSON library. TO do that add to yuor dependencies in build.gradle and sync
compile 'com.google.code.gson:gson:2.3'
假设 JSON
包含您的JSON字符串你可以使用这样的:
Supposing json
contains your JSON string you can use it like this:
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
// (...)
Gson gson = new Gson();
Type type = new TypeToken<Map<String, List<String>>>() {}.getType();
Map<String, List<String>> map = gson.fromJson(json, type);
这篇关于解析格式地图&LT的JSON;弦乐,ArrayList的&LT;串GT;&GT;在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!