我想从下面的 json 格式数据中有效地读取每个元素并将每个 json 元素存储在一个数组列表中
getData = {"block_id":"2","body":"<p>json data" }
String id = getData.getString("block_id");
String name = getData.getString("body");
我想从下面的 json 格式数据中有效地读取每个元素并将每个 json 元素存储在一个数组列表中
getData = {"block_id":"2","body":"<p>json data" }
String id = getData.getString("block_id");
String name = getData.getString("body");
您可以使用以下代码检索 json 数据
HttpParams httpParams = new BasicHttpParams(); HttpClient 客户端 = 新 DefaultHttpClient(httpParams);
String url = your url;
Log.v(TAG, "URL In onPreExecute"+url);
HttpPost request = new HttpPost(url);
try
{
response = client.execute(request);
HttpEntity entity = response.getEntity();
ent=entity.toString();
if(entity!=null)
{
InputStream instream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
builder = new StringBuilder();
String line = null;
while((line=reader.readLine())!=null)
{
builder.append(line);
}
instream.close();
Log.v("CMH", "Result of http " + builder.toString());
}
else
{
Log.v("Data", "Failed to download file");
}
JSONObject jsonObject = new JSONObject(builder.toString());
// String csSuccess = jsonObject.getString("vidlist");
jsonarray = jsonObject.getJSONArray("vidlist");
NumOfObjects = jsonarray.length();
Log.v(TAG, "NumOfObjects "+NumOfObjects);
for(int Video = 0; Video < NumOfObjects; Video++)
{
SetterGetter setget = new SetterGetter();
video_obj = jsonarray.getJSONObject(Video);
String id = video_obj.getString("video_id"));
}
ArrayList 声明,
private ArrayList<Integer> idList = new ArrayList<Integer>();
private ArrayList<String> nameList = new ArrayList<String>();
成功解析数据后,
String id = getData.getString("block_id");
String name = getData.getString("body");
//Here added into arraylist...
idList.add(id);
nameList.add(name);
你可以试试下面的代码
JSONObject json = new JSONObject(jsonstring);
String id = json.getString("block_id");
String name = json.getString("body");
ArrayList<String> arrIds = new ArrayList<String>();
arrIds.add(id);