Multi Level Json Parsing
I am writing multi level json parsing program, and able to get first level list, but not showing second level list while do tap on first level list item. In short, i am listing ca
Solution 1:
You need to pass the json array to another activity
Then
try
{
JSONArray jr = jb.getJSONArray("categories");
for(int i=0;i<jr.length();i++)
{
JSONObject jb1 = jr.getJSONObject(i);
JSONArray jr1 = jb1.getJSONArray("videos");
for(int j=0;j<jr1.length();j++)
{
JSONObject jb2 = jr1.getJSONObject(j);
String title =(String) jb2.get("title");
String thumb = (String) jb2.getString("thumb");
Log.i(".........",title);
Log.i(".........",thumb);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
The log
02-25 02:00:38.192: I/.........(1194): 2010 Day 1 Keynote
02-25 02:00:38.192: I/.........(1194): http://www.androidbegin.com/tutorial/flag/unitedstates.png
02-25 02:00:38.202: I/.........(1194): 2010 Day 2 Keynote
02-25 02:00:38.202: I/.........(1194): http://www.androidbegin.com/tutorial/flag/russia.png
02-25 02:00:38.202: I/.........(1194): Uploading your App
02-25 02:00:38.202: I/.........(1194): http://www.androidbegin.com/tutorial/flag/russia.png
02-25 02:00:38.202: I/.........(1194): Getting Started with Apps for the Chrome Web Store
02-25 02:00:38.212: I/.........(1194): http://www.androidbegin.com/tutorial/flag/unitedstates.png
Edit;
MainActivity.java
ListView lv = (ListView) findViewById(R.id.listView1);
ArrayList<HashMap<String,String>> arraylist= newArrayList<HashMap<String,String>>();
try {
// Locate the array name in JSONJSONObject jb= newJSONObject("your json");
jsonarray = jb.getJSONArray("categories");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = newHashMap<String, String>();
JSONObject 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();
}
String[] from = { "name","thumb" };
int[] to = { R.id.textView1,R.id.textView2 };
ListAdapter adapter = newSimpleAdapter(this,arraylist,R.layout.njk,from,to);
lv.setAdapter(adapter);
lv.setOnItemClickListener(newOnItemClickListener()
{
@OverridepublicvoidonItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
JSONObject jb;
try {
Intent intent = newIntent(MainActivity.this,SecondActivity.class);
jb = jsonarray.getJSONObject(arg2);
intent.putExtra("key",jb.toString());
startActivity(intent);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
SecondActivity.java
publicclassSecondActivityextendsActivity {
ListView lv;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
lv= (ListView) findViewById(R.id.listView1);
ArrayList<HashMap<String,String>> arraylist= newArrayList<HashMap<String,String>>();
try
{
String value = getIntent().getStringExtra("key");
JSONObject jb = newJSONObject(value);
JSONArray jr = jb.getJSONArray("videos");
for(int j=0;j<jr.length();j++)
{
HashMap<String,String> map = newHashMap<String,String>();
JSONObject jb2 = jr.getJSONObject(j);
String title =(String) jb2.get("title");
String thumb = (String) jb2.getString("thumb");
map.put("title",title);
map.put("thumb",thumb);
Log.i(".........",title);
Log.i(".........",thumb);
arraylist.add(map);
}
}catch(Exception e)
{
e.printStackTrace();
}
String[] from = { "title","thumb" };
int[] to = { R.id.textView1,R.id.textView2 };
ListAdapter adapter = newSimpleAdapter(this,arraylist,R.layout.njk,from,to);
lv.setAdapter(adapter);
}
njk.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/textView1"android:layout_below="@+id/textView1"android:layout_marginTop="26dp"android:text="TextView" /><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginTop="28dp"android:text="TextView" /></RelativeLayout>
activity_main.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" ><ListViewandroid:id="@+id/listView1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true" ></ListView></RelativeLayout>
activity_second.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SecondActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
Snaps
Post a Comment for "Multi Level Json Parsing"