Android 如何pase本地JSON并填充RecyclerView?
我对如何解析本地Json感到困惑。我试了很多,但找不到解决办法。Android 如何pase本地JSON并填充RecyclerView?,android,json,android-recyclerview,Android,Json,Android Recyclerview,我对如何解析本地Json感到困惑。我试了很多,但找不到解决办法。 我想填充一个只包含一个简单标题的ArrayList——我想将它从JSON添加到RecylerView { "hollywood": [{ "_id": "58d3264f93f24d2bc87bebf5", "title": "sagar rana", "image": "encrypted-tbn1.gstatic.com/...", "genre": "h
我想填充一个只包含一个简单标题的ArrayList——我想将它从JSON添加到RecylerView
{
"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);
}
}
导入android.support.v7.app.app活动;
导入android.os.Bundle;
导入android.support.v7.widget.LinearLayoutManager;
导入android.support.v7.widget.RecyclerView;
导入android.util.Log;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
导入java.util.HashMap;
导入静态android.R.attr.data;
公共类MainActivity扩展了AppCompatActivity{
回收视图回收视图;
MyAdapter回收适配器;
私人ArrayList结婚名单;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
试一试{
JSONObject obj=新的JSONObject(loadJSONFromAsset());
JSONArray m_jArry=obj.getJSONArray(“好莱坞”);
ArrayList formList=新的ArrayList();
哈什曼·穆利;
for(int i=0;i”,jo_inside.getString(“好莱坞”);
字符串公式_value=jo_inside.getString(“好莱坞”);
字符串url_value=jo_inside.getString(“url”);
//在“ArrayList”中添加值,如下所示:
m_li=新HashMap();
m_li.put(“公式”,公式值);
m_li.put(“url”,url_值);
表格列表。添加(m_li);
}
}捕获(JSONException e){
e、 printStackTrace();
}
recycleradapter=新的MyAdapter(mArrayList);
recyclerView.setAdapter(recycleradapter);
recyclerViewstart();
loadJSONFromAsset();
}
私有字符串loadJSONFromAsset(){
字符串json=null;
试一试{
InputStream is=getAssets().open(“homepage.json”);
int size=is.available();
字节[]缓冲区=新字节[大小];
is.read(缓冲区);
is.close();
json=新字符串(缓冲区,“UTF-8”);
}捕获(IOEX异常){
例如printStackTrace();
返回null;
}
返回json;
}
私有void recyclerivewstart(){
recyclerView=(recyclerView)findViewById(R.id.recylce);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager LayoutManager=新的LinearLayoutManager(此);
recyclerView.setLayoutManager(layoutManager);
}
}
如图所示,这应该可以做到:
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);
使用可以找到的org.json
库试试这个简单的方法
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;
}
这就是你的适配器
recycleradapter = new MyAdapter(mArrayList);
recyclerView.setAdapter(recycleradapter);
您从未初始化过婚姻列表。。。那就先这么做吧
mArrayList = new ArrayList<Moviedemo>();
处理JSON对于任何Android应用程序来说都是最重要的一步。 根据我的建议,尝试了解GSON图书馆 这些是一些有用的链接,您可以在其中更好地了解它。 1.
2. {“hollywood”:[{“id”:“58d3264f93f24d2bc87bebf5”,“标题”:“sagar rana”,“图像”:“流派”:“好莱坞”,“描述”:“仅用于演示目的”,“发布”:“2010”,“下载”:“无下载”,“youtube”:“,”上传”:“2017-03-23T01:35:11.182Z”}]您可以使用Gson来解析json@BhupinderSingh添加帖子而不是评论我对jsonJSON不熟悉,所以这段代码的问题到底在哪里?Android内置了
org.json
,所以mvnrepository没有必要。这是真的,我忘了。
// 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();