1
我正在寫多級json解析程序,並且能夠獲得第一級列表,但不能在顯示第一級列表項時顯示第二級列表。多級JSON解析
總之,我列出了類別正在面臨的問題,當試圖列出挖掘類別的視頻。
我做這樣的事情:
Main Activity [Listing Categories] > Another Activity [Listing Videos]
MainActivity.java:
static String NAME = "name";
static String THUMB = "thumb";
.............................
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("categories");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("name", jsonobject.getString("name"));
map.put("thumb", jsonobject.getString("thumb"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
MainAdapter.java:
// Capture ListView item click
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Get the position
resultp = data.get(position);
Intent intent = new Intent(context, AnotherActivity.class);
// Pass name
intent.putExtra("name", resultp.get(MainActivity.NAME));
// Start AnotherActivity Class
context.startActivity(intent);
}
});
return itemView;
}
注:使用上面的代碼中得到分類清單
AnotherActivity.java-
static String THUMB = "thumb";
static String TITLE = "title";
static String SUBTITLE = "subtitle";
static String DESCRIPTION = "description";
........................
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("videos");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
final JSONArray sources = jsonobject.getJSONArray("sources");
// Retrive JSON Objects
map.put("title", jsonobject.getString("title"));
map.put("thumb", jsonobject.getString("thumb"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
的Manifest.xml: -
<activity android:name="com.example.zing.AnotherActivity" />
JSON:
{
"categories": [
{
"name": "Dev Events",
"thumb": "http://www.androidbegin.com/tutorial/flag/unitedstates.png",
"videos": [
{
"sources": [
"http://commondatastorage.googleapis.com/gtv_template_assets/IO2010-Keynote-day1.mp4"
],
"thumb": "http://www.androidbegin.com/tutorial/flag/unitedstates.png",
"title": "2010 Day 1 Keynote",
"subtitle": "Dev Events",
"description": "IO2010 Keynote"
},
{
"sources": [
"http://commondatastorage.googleapis.com/gtv_template_assets/IO2010-Keynote-day2-android.mp4"
],
"thumb": "http://www.androidbegin.com/tutorial/flag/russia.png",
"title": "2010 Day 2 Keynote",
"subtitle": "Dev Events",
"description": "IO2010 Keynote Android"
}
]
},
{
"name": "Technology",
"thumb": "http://www.androidbegin.com/tutorial/flag/russia.png",
"videos": [
{
"sources": [
"http://commondatastorage.googleapis.com/gtv_template_assets/CWS-HowTo.mp4"
],
"thumb": "http://www.androidbegin.com/tutorial/flag/russia.png",
"title": "Uploading your App",
"subtitle": "Technology",
"description": "CWS HowTo"
},
{
"sources": [
"http://commondatastorage.googleapis.com/gtv_template_assets/CWS-GettingStarted.mp4"
],
"thumb": "http://www.androidbegin.com/tutorial/flag/unitedstates.png",
"title": "Getting Started with Apps for the Chrome Web Store",
"subtitle": "Technology",
"description": "Arne Roomann-Kurrik"
}
]
}
]
}
您解析看起來不錯。你在說什麼第二級 – Raghunandan
@Raghunandan上市視頻第二級 – Sun
你需要通過類別jsonarray循環。代碼 – Raghunandan