希望你们能向我解释,我是第一次使用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();
}
答案 0 :(得分:1)
在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"
}
]