gpt4 book ai didi

android - 如何从 json 对象数组中通过 id 获取特定对象?

转载 作者:行者123 更新时间:2023-11-29 22:56:20 24 4
gpt4 key购买 nike

希望你们能给我解释一下,我是第一次做这个 JSON,还有 2 天的时间来完成。

        bookByID = (TextView) findViewById(R.id.tvBookbyid);
btSearch = (Button) findViewById(R.id.btSearchbook);
etId = (EditText) findViewById(R.id.etID);
queue = Volley.newRequestQueue(this);

btSearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
loadBook();
}
});
}

private static final String JSON_URL = "deleted";

这是方法:

    private void loadBook() {
queue = Volley.newRequestQueue(this);
int id = Integer.parseInt((String) etId.getText().toString());
StringRequest request = new StringRequest(Request.Method.GET, JSON_URL + id, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {

抱歉这个间距,但它一直说我必须解释所有代码

                    JSONObject object=new JSONObject(response);
for(int i=0;i<object.length();i++) {
int bookid = object.getInt("id");
String title =object.getString("title");
String isbn =object.getString("isbn");
String description =object.getString("description");
int price =object.getInt("price");
String currencyCode =object.getString("currencyCode");
String author =object.getString("author");
list.add(new Book(bookid,title,isbn,description,price,currencyCode,author));
// Toast.makeText(getApplicationContext(), "Hello end", Toast.LENGTH_SHORT).show();
}

最佳答案

在for循环中通过id查找对象

    private void lookForJsonObj(JSONArray bookArray,int id) throws JSONException {
for (int i = 0; i < bookArray.length(); i++){
JSONObject book = bookArray.getJSONObject(i);
int bookId = book.getInt("id");
if (bookId == id){
//this is the book you are looking for
//break;
}
}
}

编辑:用这个更新你的解析代码

              JSONArray array = new JSONArray(response);
for(int i=0;i<array.length();i++) {
JSONObject obj = array.getJSONObject(i);
int bookid = obj.getInt("id");
String title =obj.getString("title");
String isbn =obj.getString("isbn");
String description =obj.getString("description");
int price =obj.getInt("price");
String currencyCode =obj.getString("currencyCode");
String author =obj.getString("author");
list.add(new Book(bookid,title,isbn,description,price,currencyCode,author));
// Toast.makeText(getApplicationContext(), "Hello end", Toast.LENGTH_SHORT).show();
}

由 OP 编辑​​:

[
{
"id": 100,
"title": "Code Complete: A Practical Handbook of Software Construction",
"isbn": "978-0735619678",
"price": 2954,
"currencyCode": "EUR",
"author": "Mike Riley"
},
{
"id": 200,
"title": "The Pragmatic Programmer",
"isbn": "978-0201616224",
"price": 3488,
"currencyCode": "EUR",
"author": "Andrew Hunt and Dave Thomas"
},
{
"id": 300,
"title": "iOS Forensic Analysis",
"isbn": "978-1430233428",
"price": 4604,
"currencyCode": "EUR",
"author": "Sean Morrisey"
},
{
"id": 400,
"title": "Ghost in the Wires: My Adventures as the World's Most Wanted Hacker",
"isbn": "978-0316037709",
"price": 1493,
"currencyCode": "EUR",
"author": "Kevin Mitnick"
},
{
"id": 500,
"title": "Handling Unexpected Errors",
"isbn": "978-0590353403",
"price": 1399,
"currencyCode": "GBP",
"author": "Charles R. Ash"
},
{
"id": 600,
"title": "Android Application Development For Dummies",
"isbn": "978-0470770184",
"price": 1979,
"currencyCode": "USD",
"author": "Donn Felker"
}
]

关于android - 如何从 json 对象数组中通过 id 获取特定对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57395243/

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