簡體   English   中英

JSON Array get JsonObject 總是返回空

[英]JSON Array get JsonObject always returns empty

我最近在使用 org.json 時遇到了一個問題。 我用以下文本制作了一個 json 數組:

 [{"name":"Stk.","unitNumber":1,"self":"https://link.com/units/1","products":"https://link.com/units/1/products"},{"name":"timer","unitNumber":2,"self":"https://link.com/units/2","products":"https://link.com/units/2/products"},{"name":"Stk.","unitNumber":3,"self":"https://link.com/units/3","products":"https://link.com/units/3/products"},{"name":"Kg","unitNumber":4,"self":"https://link.com/units/4","products":"https://link.com/units/4/products"},{"name":"Kg","unitNumber":5,"self":"https://link.com/units/5","products":"https://link.com/units/5/products"},{"name":"Km","unitNumber":6,"self":"https://link.com/units/6","products":"https://link.com/units/6/products"},{"name":"Liter","unitNumber":7,"self":"https://link.com/units/7","products":"https://link.com/units/7/products"},{"name":"Meter","unitNumber":8,"self":"https://link.com/units/8","products":"https://link.com/units/8/products"},{"name":"M2","unitNumber":9,"self":"https://link.com/units/9","products":"https://link.com/units/9/products"},{"name":"MM","unitNumber":10,"self":"https://link.com/units/10","products":"https://link.com/units/10/products"},{"name":"Meter","unitNumber":11,"self":"https://link.com/units/11","products":"https://link.com/units/11/products"},{"name":"Par","unitNumber":13,"self":"https://link.com/units/13","products":"https://link.com/units/13/products"},{"name":"Stk.","unitNumber":14,"self":"https://link.com/units/14","products":"https://link.com/units/14/products"},{"name":"Stk.","unitNumber":15,"self":"https://link.com/units/15","products":"https://link.com/units/15/products"},{"name":"Sæt","unitNumber":16,"self":"https://link.com/units/16","products":"https://link.com/units/16/products"},{"name":"Sæt","unitNumber":17,"self":"https://link.com/units/17","products":"https://link.com/units/17/products"},{"name":"\‎","unitNumber":19,"self":"https://link.com/units/19","products":"https://link.com/units/19/products"},{"name":"Stk.","unitNumber":20,"self":"https://link.com/units/20","products":"https://link.com/units/20/products"},{"name":"Stk.","unitNumber":21,"self":"https://link.com/units/21","products":"https://link.com/units/21/products"},{"name":"L","unitNumber":22,"self":"https://link.com/units/22","products":"https://link.com/units/22/products"}]

現在,我運行以下代碼

            for (int i = 0; i < jsonArray.length(); i++){
                JSONObject jsonObject1 = new JSONObject(jsonArray.getJSONObject(i));
                System.out.print("\n"+jsonObject1.toString());
            }

我得到

{“空”:假}

每次。

我究竟做錯了什么?

您可以嘗試下面的代碼並用 try catch for JSONException 包圍它

for (int i = 0; i < jsonArray.length(); i++){
    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
    System.out.print("\n"+jsonObject1.toString());
}

我試圖重現您的問題,但它有效。 我正在使用以下方法。

  • 使用您的數據構建 JSONObject(此處僅使用列表中的兩項)
  • 創建一個 JSONArray
  • 然后將這些創建的對象放入 JSONArray
org.json.JSONArray jsonArray = new org.json.JSONArray();

org.json.JSONObject jsonObj = new org.json.JSONObject();
jsonObj.put("name", "Stk");
jsonObj.put("unitNumber", 1);
jsonObj.put("self", "https://link.com/units/1");
jsonObj.put("products", "https://link.com/units/1/products");
jsonArray.put(jsonObj);

jsonObj = new org.json.JSONObject();
jsonObj.put("name", "timer");
jsonObj.put("unitNumber", 2);
jsonObj.put("self", "https://link.com/units/2");
jsonObj.put("products", "https://link.com/units/2/products");
jsonArray.put(jsonObj);

for(int i=0; i < jsonArray.length();i++) {
    org.json.JSONObject jsonObject1 = (org.json.JSONObject)jsonArray.get(i);
    System.out.println(jsonObject1);
}

輸出:

{"name":"Stk","unitNumber":1,"self":"https://link.com/units/1","products":"https://link.com/units/1/products"}
{"name":"timer","unitNumber":2,"self":"https://link.com/units/2","products":"https://link.com/units/2/products"}

暫無
暫無

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

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