2017-08-30 83 views
-2
{ 

"pagination": 
    {"next_max_tag_id": "AQCXof9d52l0OUMJPHt-mSJxx-p-zuH7GGDLnBv_3DhqR9LiBi8rgTw0HqAv6zHzWyar9C5_yUOmgqoZzB39OmNFELBEW7xBjkDm1tmff0RB52vJKYWt73wg61ww2XvrxCQ", 
     "next_max_id": "AQCXof9d52l0OUMJPHt-mSJxx-p-zuH7GGDLnBv_3DhqR9LiBi8rgTw0HqAv6zHzWyar9C5_yUOmgqoZzB39OmNFELBEW7xBjkDm1tmff0RB52vJKYWt73wg61ww2XvrxCQ", 
     "next_url": "https://api.instagram.com/v1/tags/love/media/recent?access_token=2121718149.3444044.98426ea4ac8445dc83aaace64b189ad2\u0026max_tag_id=AQCXof9d52l0OUMJPHt-mSJxx-p-zuH7GGDLnBv_3DhqR9LiBi8rgTw0HqAv6zHzWyar9C5_yUOmgqoZzB39OmNFELBEW7xBjkDm1tmff0RB52vJKYWt73wg61ww2XvrxCQ", 

     "next_min_id": "AQCNS5gC4sFLM5TDxwhOOBXYXU2NQGrGSaSLhPResOyHEeT5Pse_17otIZa6gpZaUFQqxNHTokaMH1ZIQt2Gbm3xur52blWeChQs3LDbqhOa8wRwFVHTVwTd79-12uWF3Ck", "min_tag_id": "AQCNS5gC4sFLM5TDxwhOOBXYXU2NQGrGSaSLhPResOyHEeT5Pse_17otIZa6gpZaUFQqxNHTokaMH1ZIQt2Gbm3xur52blWeChQs3LDbqhOa8wRwFVHTVwTd79-12uWF3Ck", 
     "deprecation_warning": "next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead" 
} 

, 


"data": 

     [ 
     { 
      "id": "1592546164972439952_5948080381", 
      "user": 
        { 
        "id": "5948080381", 
        "full_name": "From America To Africa LDR \u2764\ufe0f", 
         "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/21148153_1465133843555656_2623564353101627392_a.jpg", 
         "username": "fromamericatoafrica17" 
        }, 


      "images": 
        { 
        "thumbnail": 
          { 
           "width": 150, 
           "height": 150, 
           "url": "https://scontent.cdninstagram.com/t51.2885-15/s150x150/e35/c0.78.770.770/21147744_266508440508403_5257290165033893888_n.jpg" 
          }, 
        "low_resolution": 
          { 
           "width": 320, 
           "height": 384, 
          "url": "https://scontent.cdninstagram.com/t51.2885-15/e35/p320x320/21147744_266508440508403_5257290165033893888_n.jpg" 
          }, 
        "standard_resolution": 
          { 
           "width": 640, 
           "height": 769, 
           "url": "https://scontent.cdninstagram.com/t51.2885-15/sh0.08/e35/p640x640/21147744_266508440508403_5257290165033893888_n.jpg" 
          } 
      }, 

     "created_time": "1504066318", 

     "caption": 
        { 
        "id": "17890734748067049", 
        "text": "Love it. Just love it. .\n#ldr #longdistancerelationship #puppylove #love #fromamericatoafrica", 
        "created_time": "1504066318", 
        "from": 
          { 
           "id": "5948080381", 
           "full_name": "From America To Africa LDR \u2764\ufe0f", 
           "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/21148153_1465133843555656_2623564353101627392_a.jpg", 
           "username": "fromamericatoafrica17" 
           } 
        }, 

     "user_has_liked": false, 

     "likes": {"count": 0}, 



      "tags": 
        [ 
         "ldr", "longdistancerelationship", "love", "puppylove", "fromamericatoafrica"], 



     "filter": "Normal", "comments": {"count": 0}, 

     "type": "image", 
     "link": "https://www.instagram.com/p/BYZ3Eyjl8mQ/", 
    "location": null, "attribution": null, "users_in_photo": [] 


     }, 

我需要從此JSON訪問用戶名,資料圖片。Json解析器從Instagram的API api機器人

private class Insta_search extends AsyncTask<String, Void, String> { 

     @Override 
     protected String doInBackground(String... params) { 

      String uri = params[0]; 
      BufferedReader bufferedReader = null; 
      try { 
       URL url = new URL(uri); 
       HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
       StringBuilder sb = new StringBuilder(); 

       bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream())); 

       String json; 
       while ((json = bufferedReader.readLine()) != null) { 
        sb.append(json + "\n"); 
       } 

       return sb.toString().trim(); 


      } catch (Exception e) { 
       return null; 
      } 
     } 

     protected void onPostExecute(String s) { 

      super.onPostExecute(s); 
      //PENDING_STUDENT_NAME.clear(); 


      if (s != null) { 
       try { 
        // JSONArray arr = new JSONArray(s); 
        JSONObject jsonObj = (JSONObject) new JSONTokener(s) 
          .nextValue(); 

        String name = jsonObj.getJSONObject("user").getString("id"); 
        // DataModel mDatModel = new DataModel(); 
} 
} 

這是我的代碼..

回答

0

您可以查看下面的代碼。

JsonArray responseArray = new JsonArray(response); 
for(int i = 0; i < responseArray.length(); i++) { 
    JsonObject object = responseArray.getJsonObject(i); 
    JsonObject userObject = object.getJsonObject("user"); 
    String userName = userObject.getString("username"); 
    String profile_pictur = userObject.getString("profile_picture"); 
} 

Check this link for more information on Json parsing

0

更新您的onPostExecute,如:

protected void onPostExecute(String s) { 

    super.onPostExecute(s); 
    if (s != null) { 
     try { 
      JSONObject jsonObj = (JSONObject) new JSONTokener(s) 
        .nextValue(); 
      JSONArray dataArray = jsonObj.getJSONArray("data"); 
      JSONObject picObj = dataArray.get(0); 
      JSONObject userObj = picObj.getJSONObject("user"); 
      String name = userObj.getString("full_name"); 
     } catch(Exception e) { 

     } 
    } 
} 

而且你還沒有公佈完整的JSON。

0

試試這個,

protected void onPostExecute(String s) { 

    super.onPostExecute(s); 

    if (s != null) 
    { 
     try { 
      JSONObject jsonObj = new JSONObject(s); 
      JSONArray dataArray = jsonObj.getJSONArray("data"); 
      for(int i=0;i<dataArray.length();i++) 
      { 
       JSONObject jsonObj_user_data = dataArray.get(i); 
       String name = jsonObj_user_data.getJSONObject("user").getString("id"); 
       String full_name = jsonObj_user_data.getJSONObject("user").getString("full_name"); 
       String profile_picture = jsonObj_user_data.getJSONObject("user").getString("profile_picture"); 
       String username = jsonObj_user_data.getJSONObject("user").getString("username"); 
      } 
     } 
     catch(Exception e) { 

     } 
    } 
}