0

我有一个 web 服务的响应来获取提要列表,在该响应中我得到了两个具有相同名称的数组。问题是我无法获得内部数组的详细信息,即“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;
    }
  }

请帮助我获取第二个数组的详细信息。

任何帮助将不胜感激。

谢谢你。

4

6 回答 6

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());}
于 2013-08-23T11:15:01.190 回答
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();
        }
于 2013-08-23T10:04:28.973 回答
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);

  }

当然,您必须将您的 abcd 收藏从 String 更改为 InfoHolder

于 2013-08-23T10:00:34.700 回答
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;
  }

}

于 2013-08-23T10:03:26.967 回答
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();

    }
于 2013-08-23T10:06:16.197 回答
0

你做

for (int i = 0; i < ja.length(); i++) {

好的第一个数组现在你需要在第一个的每个单元格中获取数组

例如 :

JSONObject jo = ja.getJSONObject(i);

JSONArray ja2 = jo.getJSONArray("result");

然后只有你做第二个循环

也许这个答案不完整,但想法就在这里

于 2013-08-23T10:02:39.540 回答