簡體   English   中英

如何從json對象數組中通過id獲取特定對象?

[英]how to get specific object by id from json array of objects?

希望你們能解釋一下,我是第一次做這個 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"
  }
]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM