2016-10-23 23 views
0

以下に添付されているjsonの応答があります。私はアンドロイドのボレーでこの応答を解析したい。配列内のオブジェクトをどのように解析するのですか?JSON配列内のJSON配列をandroid volleyライブラリで解析します。

 
    { 
    "status": "ok", 
    "source": "techcrunch", 
    "sortBy": "top", 
    "articles": [ 
    { 
     "author": "Ingrid Lunden, Fitz Tepper", 
     "title": "Confirmed: AT&T is buying Time Warner for $85.4B in cash and shares", 
     "description": "After days of speculation, the deal is now official: AT&T is acquiring Time Warner for $85 billion in a mix of cash and shares, paving the way for..", 
     "url": "http://social.techcrunch.com/2016/10/22/confirmed-att-is-buying-time-warner-for-85-4b-in-cash-and-shares/", 
     "urlToImage": "https://tctechcrunch2011.files.wordpress.com/2016/10/946_432_newsroom_release_tw.jpg?w=764&h=400&crop=1", 
     "publishedAt": "2016-10-23T00:02:34Z" 
    }, 

私は最初のオブジェクトにアクセスし、次のオブジェクトにアクセスしたい。感謝する。

+0

あなたの結果を得るためにこの方法を試してみてください、あなたは、サーバからJSONを取得するためにボレーを使用する方法を知っていますか? –

+0

あなたは@RitikKumarAgrahari –

+0

私は、それを見て解決策を掲載したことを教えてください –

答えて

3

これはタイトルあなたが

コンパイル 'com.mcxiaoke.volley:ライブラリ-AAR:1.0.0' を使用してボレーライブラリを追加する必要が最初

JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url, null, 
    new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 
      JSONArray jsonArray = null; 
      try { 
       jsonArray = response.getJSONArray("articles"); 
       for(int i=0; i<jsonArray.length(); i++){ 
        JSONObject jsonObject = (JSONObject) jsonArray.get(i); 
         Log.d(TAG, jsonObject.getString("title")); 
        } 
       } catch (JSONException e) { 
         e.printStackTrace(); 
       }      } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.d(TAG, "Error: " + error.getMessage()); 
     } 
    }); 
+0

@短い回答を働いていません。実行後にブランク画面を返し、ログcatのレスポンスコード405を返します –

+0

* Request.Method.POST *から* Request.Method.GET *への応答が更新されました。 –

0

の一覧表示されるはずです

をbuild.grdleファイル内

apply plugin: 'com.android.application' 

android { 
compileSdkVersion 24 
buildToolsVersion "24.0.2" 

defaultConfig { 
    applicationId "com.iitism.ritik.popularmovies" 
    minSdkVersion 15 
    targetSdkVersion 24 
    versionCode 1 
    versionName "1.0" 
} 
buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
} 

dependencies { 
compile fileTree(include: ['*.jar'], dir: 'libs') 
testCompile 'junit:junit:4.12' 
compile 'com.android.support:appcompat-v7:24.1.1' 
compile 'com.android.support:design:24.1.1' 
compile 'com.mcxiaoke.volley:library-aar:1.0.0' 
compile 'com.squareup.picasso:picasso:2.5.2' 
compile files('libs/YouTubeAndroidPlayerApi.jar') 
} 

の下に表示のように続いてuはJSONオブジェクト01を取得するURLが必要あなたのJSONにuはuが

JSONArray jsonArray = jsonObject.getJSONArray("articles"); 

int n = jsonArray.length(); 
    for(int i=0;i<n;i++) 
    { 
     JSONObject movieObject = jsonArray.getJSONObject(i); 
     //do your work here 
    } 
0

まずあなたはこのAPIは、POSTでチェックコードの下に使用できるように、「記事」の配列を解析するようその後Uは、JSONに

StringRequest stringRequest = new StringRequest(URL, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      Log.d("TAG",response); 
      showJson(response); 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Toast.makeText(MainActivity.this,error.getMessage(),Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 


public void showJson(String response) 
{ 
    Log.d("TAG",response); 
    try { 
     JSONObject jsonObject = new JSONObject(response); 
     JSONArray jsonArray = jsonObject.getJSONArray("results"); 
     int n = jsonArray.length(); 
     for(int i=0;i<n;i++) 
     { 
      JSONObject movieObject = jsonArray.getJSONObject(i); 
      String title = movieObject.getString("original_title"); 
      String poster_path = movieObject.getString("poster_path"); 
      String overView = movieObject.getString("overview"); 
      String releaseDate = movieObject.getString("release_date"); 
      String popularity = movieObject.getString("popularity"); 
      String voteAvg = movieObject.getString("vote_average"); 
      String id = movieObject.getString("id"); 
      movieList.add(new Movie(poster_path,title,overView,releaseDate,popularity,voteAvg,id)); 
      movieAdapter.notifyDataSetChanged(); 
     } 
    } catch (JSONException e) { 
     Toast.makeText(getApplicationContext(),"Not available",Toast.LENGTH_SHORT).show(); 
     e.printStackTrace(); 
    } 
} 

を解析するために、コードの下に従うことができますAPIまたはGET API。このapiがPOSTメソッドであれば、ハッシュマップにパラメータを渡し、GET APIの場合はurlでパラメータを渡します。コードの下にあなたの与えられたjsonを解析しています。

 StringRequest notificationrequest = new StringRequest(Request.Method.POST, YOUR_URL, 
        new Response.Listener<String>() { 
         @Override 
         public void onResponse(String response) { 



          try { 


           JSONObject jObject = new JSONObject(response); 
           if (jObject.getString("status").equals("ok")) { 

           String source = jObject.getString("source"); 
           String sortby = jObject .getString("sortBy"); 



            JSONArray jsonArray = jObject.getJSONArray("articles"); 
            for (int i = 0; i < jsonArray.length(); i++) { 


             JSONObject jsonObject = jsonArray.getJSONObject(i); 

        String author = jsonObject.getString("author"); 
        String title= jsonObject.getString("title"); 
        String description= jsonObject.getString("description"); 
        String url= jsonObject.getString("url"); 
        String urlToImage= jsonObject.getString("urlToImage");  
        String publishedAt= jsonObject.getString("publishedAt");          




               } 





           } else { 

           } 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 
        }, 
        new Response.ErrorListener() { 
         @Override 
         public void onErrorResponse(VolleyError volleyError) { 

          Log.e("error", "" + volleyError.getMessage()); 


         } 
        }) { 
       @Override 
       protected Map<String, String> getParams() throws AuthFailureError { 

        Map<String, String> params = new HashMap<String, String>(); 
        //put your parameter in the hash map variable params if you using post request 


        return params; 
       } 
      }; 

      RequestQueue notificationqueue = Volley.newRequestQueue(getContext()); 

      notificationqueue.add(notificationrequest); 

そして、ボレーの階調依存性を忘れないでください。

0

JsonObjectRequest obreq = new JsonObjectRequest(Request.Method.GET, url, new 

      Response.Listener<JSONObject>() { 

       @Override 
       public void onResponse(JSONObject response) { 
        try { 
         JSONArray obj = response.getJSONArray("articles"); 

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

          JSONObject jsonObject = obj.getJSONObject(i); 



          String type = jsonObject.getString("author"); 

          // check the other values like this so on.. 

         } 

        } 
        catch (JSONException e) { 

         e.printStackTrace(); 
        } 
       } 
      },null); 
0
public void onResponse(Object response) { 
      JSONArray jsonArray = null; 
      try { 
      Log.e("status",((JSONObject)response).getString("status")); 
      Log.e("source",((JSONObject)response).getString("source")); 
      Log.e("sortBy",((JSONObject)response).getString("sortBy")); 
      Log.e("articles",((JSONObject)response).getString("articles"));//Notice than the articles string could not be completly display in the Log if it is too long 

       //if you want to browse the table of articles 
       jsonArray = ((JSONObject)response).getJSONArray("articles"); 
       for (int i = 0 ; i < jsonArray.length() ; i++){ 
        Log.e("Item "+i,jsonArray.getJSONObject(i).getString("source")); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
    }