我有一个看起来像下面给出的json结构的json文件,现在我希望所有item_id = 2的对象都将它们传递给recyclerview。有人可以解决我的问题吗?预先感谢
我的json文件结构如下
{
"iceberg": [
{
"item_id": 1,
"Quote": "some quote text"
},
{
"item_id": 2,
"Quote": "some quote text"
},
{
"item_id": 1,
"Quote": "some quote text"
},
{
"item_id": 2,
"Quote": "some quote text"
}
]}
答案 0 :(得分:1)
private void parseJSON() {
String url = "https://example.com/files/file.json";
int itemId = 0 //declare int for current item id
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray=response.getJSONArray("iceberg");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject hit = jsonArray.getJSONObject(i);
//set current item id
itemId = o.getInt("item_id");
//and then check if itemId equals 2 or not.
if (number == 2) {
String quote = hit.getString("Quote");
mExampleList.add(new ExampleItem(quote));
}
}
mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
mRecyclerView.setAdapter(mExampleAdapter);
mExampleAdapter.setOnItemClickListener(MainActivity.this);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQueue.add(request);
}
答案 1 :(得分:0)
您可以使用JavaScript过滤器ID。... 让myId = 2; 让subSet = jsonArray.filter(x => x.id == myId); 然后,您将编写将subSet发送到垃圾箱的代码。