我对如何解析本地Json感到困惑。我尝试了很多,但我找不到解决办法 我想填充一个只包含一个简单标题的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);
}
}
答案 0 :(得分: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();
答案 1 :(得分: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);
使用可以找到here
的org.json
库
答案 2 :(得分: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;
}
答案 3 :(得分:0)