2017-03-22 25 views
1

我對如何解析本地Json感到困惑。我嘗試了很多,但我找不到解決方案。
我想填充一個ArrayList,其中只包含一個簡單的標題 - 我想從JSON添加tite到RecylerView。如何設置本地JSON並填充RecyclerView?

{ 
    "hollywood": [{ 
     "_id": "58d3264f93f24d2bc87bebf5", 
     "title": "sagar rana", 
     "image": "encrypted-tbn1.gstatic.com/...", 
     "genre": "hollywood", 
     "description": "This for only demo purpose", 
     "release": 2010, 
     "download": "No download", 
     "youtube": "youtube.com/embed/C-5EOd8xavw...", 
     "__v ": 0, 
     "upload ": "2017-03-23T01:35:11.182Z" 
    }] 
} 

MainActivty.java

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.util.Log; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.IOException; 
import java.io.InputStream; 
import java.util.ArrayList; 
import java.util.HashMap; 

import static android.R.attr.data; 

public class MainActivity extends AppCompatActivity { 
    RecyclerView recyclerView; 
    MyAdapter recycleradapter; 
    private ArrayList<Moviedemo> mArrayList; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     try { 
      JSONObject obj = new JSONObject(loadJSONFromAsset()); 
      JSONArray m_jArry = obj.getJSONArray("hollywood"); 
      ArrayList<HashMap<String, String>> formList = new ArrayList<HashMap<String, String>>(); 
      HashMap<String, String> m_li; 

      for (int i = 0; i < m_jArry.length(); i++) { 
       JSONObject jo_inside = m_jArry.getJSONObject(i); 
       Log.d("Details-->", jo_inside.getString("hollywood")); 
       String formula_value = jo_inside.getString("hollywood"); 
       String url_value = jo_inside.getString("url"); 

       //Add your values in your `ArrayList` as below: 
       m_li = new HashMap<String, String>(); 
       m_li.put("formule", formula_value); 
       m_li.put("url", url_value); 

       formList.add(m_li); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     recycleradapter = new MyAdapter(mArrayList); 
     recyclerView.setAdapter(recycleradapter); 

     recyclerViewstart(); 
     loadJSONFromAsset(); 


    } 

    private String loadJSONFromAsset() { 
     String json = null; 
     try { 

      InputStream is = getAssets().open("homepage.json"); 

      int size = is.available(); 

      byte[] buffer = new byte[size]; 

      is.read(buffer); 

      is.close(); 

      json = new String(buffer, "UTF-8"); 


     } catch (IOException ex) { 
      ex.printStackTrace(); 
      return null; 
     } 
     return json; 


    } 

    private void recyclerViewstart() { 
     recyclerView=(RecyclerView)findViewById(R.id.recylce); 
     recyclerView.setHasFixedSize(true); 
     RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); 
     recyclerView.setLayoutManager(layoutManager); 

    } 
} 
+0

{ 「好萊塢」:[{ 「_id」: 「58d3264f93f24d2bc87bebf5」, 「標題」: 「Sagar的蛙」, 「圖像」:「https://開頭加密tbn1 .gstatic.com /圖像q = TBN:ANd9GcRGIS3UlHSRvHFV6fm8kOS5HsXI3BIY8oO1HgxHpmu2dYO6ci3-9A 「 」流派「: 」好萊塢「, 」說明「: 」這僅用於演示的目的「, 」放「: 」2010「, 」 下載「:」沒有下載「, 」youtube「:」https://www.youtube.com/embed/C-5EOd8xavw「, 」__v「:0, 」upload「:」2017-03-23T01:35 :11。182Z」 } ] –

+0

可以使用GSON與此代碼解析JSON – Sanjeet

+0

@BhupinderSingh添加它後沒有評論 –

回答

1

因此,這是您的適配器。

recycleradapter = new MyAdapter(mArrayList); 
recyclerView.setAdapter(recycleradapter); 

你永遠不會初始化mArrayList ......這樣做,第一

mArrayList = new ArrayList<Moviedemo>(); 

然後,你要添加到列表中後,確定要在JSON解析什麼。

您已經向您展示瞭如何獲取數組和字符串。

我想從JSON添加標題recyler視圖

看着這個標題?作爲一個字符串?那麼爲什麼適配器持有整個Moviedemo對象?

// Set the adapter 
recycleradapter = new MyAdapter(mArrayList); 
recyclerView.setAdapter(recycleradapter); 

// Open and parse file 
try { 
    JSONObject obj = new JSONObject(loadJSONFromAsset()); 
    JSONArray m_jArry = obj.getJSONArray("hollywood"); 

    for (int i = 0; i < m_jArry.length(); i++) { 
     // Start an object 
     Moviedemo movie = new Moviedemo(); 

     // Parse the JSON 
     JSONObject jo_inside = m_jArry.getJSONObject(i); 
     String title = jo_inside.getString("title"); 

     // Build the object 
     movie.setTitle(title); 

     // Add the object to the list 
     mArrayList.add(movie); 
    } 

} catch (JSONException e) { 
    e.printStacktrace(); 
} 

// Update adapter 
recycleradapter.notifyDataSetChanged(); 
+0

感謝兄弟它的作品,我得到了重點 –

0

正如已被證明here,這應該這樣做:

StringBuilder buf = new StringBuilder(); 
InputStream json = getAssets().open("book/contents.json"); 
BufferedReader in = 
    new BufferedReader(new InputStreamReader(json, "UTF-8")); 
String str; 

while ((str=in.readLine()) != null) { 
    buf.append(str); 
} 

in.close(); 

和你可以做這樣的事情來分析它:

JSONObject object = new JSONObject(jsonString); 

使用t他org.json庫,可以發現here

+0

Android有內置的'org.json',所以mvnrepository不是必需的。https://developer.android.com/reference/org/json/JSONObject.html –

+0

這是真的,我忘了。 – Catman155

0

試試這個簡單的方法

private JSONObject getAssetJSON() { 
     String inFile = "fileName.json"; 
     JSONObject assetJson = null; 
     try { 
      InputStream stream = context.getAssets().open(inFile); 
      int size = stream.available(); 
      byte[] buffer = new byte[size]; 
      stream.read(buffer); 
      stream.close(); 
      String assetString = new String(buffer); 
      assetJson = new JSONObject(assetString); 
     } catch (IOException e) { 
      // Handle IO exceptions here 
     } catch (JSONException e) { 
      // Handle JSON exceptions here 
     } 
     return assetJson; 
    } 
0

處理JSON是任何Android應用程序中最重要的一步。 至於我的建議,請嘗試學習GSON Library。

這些是一些有用的鏈接,您可以更好地理解它。 1。 this, 2. this