【问题标题】:how to get specific object by id from json array of objects?如何从 json 对象数组中通过 id 获取特定对象?
【发布时间】:2019-08-07 13:08:48
【问题描述】:

希望你们能给我解释一下,我是第一次做这个 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();
                    }

【问题讨论】:

    标签: android json api


    【解决方案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"
      }
    ]
    

    【讨论】:

    • 好吧,我没有正确解释,所以我想要做的是当你在我的 Android Activity 上单击一个按钮时,按钮从 EditText 获取文本,并使用作为 ID 的文本,按该 ID 打开图书。我将添加更多代码
    • 您能否毫无错误地获取图书?如果你不能,我会更新我的答案
    • 明天我会告诉你,因为我想我找到了解决办法,但现在是午夜,所以必须去睡觉,晚安;]
    • 我仍然无法成功,我的 ID 出现数字格式异常,但不知道如何在 catch 块中制作它,因为如果我制作了 catch 块,我的 URL 链接没有'看不到我的 int ID... 之后我想用 POST 测试如何传递大括号` int id = Integer.parseInt("{" + etID1.getText().toString() + "}"); requestQueue = Volley.newRequestQueue(getApplicationContext()); StringRequest stringRequest = new StringRequest(Request.Method.POST, URL + id, new Response.Listener() { `
    • 你的 json 是什么?你有一个名为“书籍”的字段吗?如果是这样,请查看我的编辑。你应该从你的“响应”JSONObject 中得到一个 JSONArray
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 2021-11-26
    相关资源
    最近更新 更多