gpt4 book ai didi

javascript - 如何通过 ID 从数组中过滤 json 对象并传递给 recyclerview

转载 作者:行者123 更新时间:2023-12-02 23:23:59 25 4
gpt4 key购买 nike

我有一个 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"
}
]}

最佳答案

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);
}

关于javascript - 如何通过 ID 从数组中过滤 json 对象并传递给 recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56820393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com