我有一个webservice的响应来获取feed列表,在那个响应中我得到了两个同名的数组。 问题是我无法获得内部数组的细节,即“ProfileName”,“ImageUrl”等。
我尝试过json解析。
以下是JSON的回复:
{"Status":true, "result": [ {"result": [ {"ProfileName":"followers5","ImageUrl":"http:\/\/192.168.0.1\/webservice2\/uploads\/81.png","Likes":3,"Hearts":2}, {"ProfileName":"followers5","VideoUrl":"http:\/\/192.168.0.1\/webservice2\/uploads\/81.mp4","Likes":0,"Hearts":0} ]} ,{"result":[ {"ProfileName":"followers6","ImageUrl":"http:\/\/192.168.0.1\/webservice2\/uploads\/82.png","Likes":0,"Hearts":2}, {"ProfileName":"followers6","VideoUrl":"http:\/\/192.168.0.1\/webservice2\/uploads\/82.mp4","Likes":0,"Hearts":0} ]} ] }
我试过如下:
class feedtask extends AsyncTask<String, Integer, String> { @Override protected String doInBackground(String... params) { Httputils getmethod = new Httputils(); try { result = getmethod.Getrequest("feeds.php?uid=76"); System.out.println("Feeds Result--->" + result); JSONObject jobj = new JSONObject(result); JSONArray ja = jobj.getJSONArray("result"); for (int i = 0; i < ja.length(); i++) { for (int j = 0; j <= i; j++) { JSONObject jo = ja.getJSONObject(j); abcd.add(jo.getString("ProfileName")); System.out.println("Profile Name--->" + jo.getString("ProfileName")); } } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } }
请帮我解决第二个阵列的详细信息。
任何帮助将不胜感激。
谢谢。
答案 0 :(得分:4)
try
{
JSONObject _JSONResponse=new JSONObject(response);
String status=_JSONResponse.getString("Status");
JSONArray _ArrayResponse=_JSONResponse.getJSONArray("result");
for(int i=0;i<_ArrayResponse.length();i++)
{
JSONArray object=_ArrayResponse.getJSONObject(i).getJSONArray("result");
for(int j=0;j<object.length();j++)
{
JSONObject jObject=object.getJSONObject(j);
ProfileName=jObject.getString("ProfileName") ;
VideoUrl=jObject.optString("VideoUrl") ;
Likes=jObject.optString("Likes") ;
Hearts=jObject.optString("Hearts") ;
Log.i(TAG,ProfileName +" "+VideoUrl +" "+Likes+" " +Hearts);
}
}
}
catch(JSONException e){Log.e(TAG,e.toString());}
答案 1 :(得分:2)
try{
JSONObject responseJson = new JSONObject(response);
if(responseJson.has("result")){
JSONArray resultJsonArr = responseJson.getJSONArray("result");
for(int i=0; i<resultJsonArr.length(); i++){
JSONObject resultInstanceJson = resultJsonArr.getJSONObject(i);
if(resultInstanceJson.has("result")){
JSONArray resultArr = resultInstanceJson.getJSONArray("result");
for(int j=0; j<resultArr.length(); j++){
JSONObject jsonResult = resultArr.getJSONObject(j);
String profileNAme = jsonResult.getString("ProfileName");
String imageUrl = "", videoUrl = "";
//image url
if(jsonResult.has("ImageUrl")){
imageUrl = jsonResult.getString("ImageUrl");
}
//video url
if(jsonResult.has("VideoUrl")){
videoUrl = jsonResult.getString("VideoUrl");
}
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
答案 2 :(得分:1)
试试这种方式
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = ja.getJSONObject(i);
JSONArray innerResult = jo.getJSONArray("result");
int size = innerResult.legth();
for (int j = 0; j < size; j++) {
JSONObject innerJo = innerResult.getJSONObject(j);
abcd.add(innerJo.getString("ProfileName"));
}
}
编辑:
public class InfoHolder {
public String profileName;
public String imageUrl;
public String videUrl;
}
for (int j = 0; j < size; j++) {
InfoHolder holder = new InfoHolder();
JSONObject innerJo = innerResult.getJSONObject(j);
// the same for imageUrl and videoUrl
holder.profileName = innerJo.getString("ProfileName");
abcd.add(holder);
}
当然你必须从String更改为InfoHolder你的abcd集合
答案 3 :(得分:1)
试试这个:
try {
result = getmethod.Getrequest("feeds.php?uid=76");
System.out.println("Feeds Result--->" + result);
JSONObject jobj = new JSONObject(result);
JSONArray ja = jobj.getJSONArray("result");
for (int i = 0; i < ja.length(); i++) {
for (int j = 0; j < i; j++) {
JSONObject jo = ja.getJSONObject(j);
JSONArray resultJA = jo.getJSONArray("result");
for(int k=0;k< resultJA.length();k++){
JSONObject jo_inside = resultJA.getJSONObject(k);
String profileName = jo_inside.getString("ProfileName");
//use optString on JSONOBject. it will return empty String if that key does not exists or else the value u want. it won't give any exceptions.
String ImageURL = jo_inside.optString("ImageUrl");
String VideoURL = jo_inside.optString("VideoUrl");
}
}
}
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
答案 4 :(得分:1)
JSONObject obj1 = new JSONObject(url);
try {
JSONArray result = obj1.getJSONArray("result");
for(int i=0;i<=result.length;i++)
{
JSONObject obj2 = result.getJSONObject(i);
JSONArray result1 = obj2.getJSONArray("result");
String ProfileName=result1.getString("ProfileName");
String ImageUrl=result1.getString("ImageUrl");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 5 :(得分:0)
你做
for (int i = 0; i < ja.length(); i++) {
好的第一个数组现在你需要在第一个
的每个单元格中获取数组例如:
JSONObject jo = ja.getJSONObject(i);
JSONArray ja2 = jo.getJSONArray("result");
然后你做第二次循环
也许这个答案并不完整,但这个想法就在这里