JSONArray mJsonArray = new JSONArray("Response");
for(int i=0;i< mJsonArray.length();i++)
{
JSONObject obj =(JSONObject) mJsonArray.get(i);
String strIp = obj.getString("ip");
arraylist.add(strIP);
}
JSON processing in Android
-
08-07-2023 - |
سؤال
[{"ip":"127.31.25.145"},{"ip":"196.32.25.256"},{"ip":"10.32.25.256"}]
This a JSON response I get from a php webpage I developed. Now I want to add all values under ip
field to a ListActivity
. I tried a lot but could do, there are many references out there but even then I couldn't do. This JSON response may contain many more ip
values. Its kind of a variable response, gets changed from time to time. Please Help!!!
المحلول
نصائح أخرى
I assume you have the json downloaded.
You need to parse the json first.
[ // json array node
{ // json object node
"ip": "127.31.25.145"
},
To parse
ArrayList<String> list = new ArrayList<String>(); // to add ip's
JSONArray jr = new JSONArray("json string");
for(int i=0;i<jr.length();i++)
{
JSONObject jb =(JSONObject)jr.get(i);
String ip = jb.getString("ip");
list.add(ip);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
//this refers to activity context
// android.R.layout.simple_list_item_1 is the layout from android framework with a textview
// http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/frameworks/base/core/res/res/layout/simple_list_item_1.xml?av=f
// the third param is your list
Since its a ListActivity
to display items
setListAdapter(adapter);
لا تنتمي إلى StackOverflow