JSONObject return null 尽管它有值
JSONObject return null although it has values
简单的4行代码:
JSONObject curr = jsonArray.getJSONObject(i);
System.out.println("the line" + curr.getString("post"));
System.out.println("id: " + curr.optString("id"));
System.out.println("name: " + curr.optString("name"));
打印以下输出:
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: the line{"id":"0","name":"פטישי חציבה"}
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: id:
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: name:
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: the line{"id":"1","name":"מקדחות יהלום"}
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: id:
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: name:
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: the line{"id":"2","name":"מהדקי אדמה"}
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: id:
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: name:
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: the line{"id":"3","name":"מכונות פוליש"}
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: id:
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: name:
有人能帮我理解如果我打印整行它是如何可能的,它很清楚每一行都有 "id" 并且还有 "name" 并且当我尝试只获取它们的值时我得到空结果?
这是 JSONArray:
[{"post":{"id":"0","name":"פטישי חציבה"}},{"post":{"id":"1","name":"מקדחות יהלום"}},{"post":{"id":"2","name":"מהדקי אדמה"}},{"post":{"id":"3","name":"מכונות פוליש"}}]
那是一个 post:
{"id":"0","name":"פטישי חציבה"}
您可能想这样做:
JSONObject curr = jsonArray.getJSONObject(i);
System.out.println("id: " + curr.optJSONObject("post").optString("id"));
System.out.println("name: " + curr.optJSONObject("post").optString("name"));
简单的4行代码:
JSONObject curr = jsonArray.getJSONObject(i);
System.out.println("the line" + curr.getString("post"));
System.out.println("id: " + curr.optString("id"));
System.out.println("name: " + curr.optString("name"));
打印以下输出:
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: the line{"id":"0","name":"פטישי חציבה"}
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: id:
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: name:
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: the line{"id":"1","name":"מקדחות יהלום"}
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: id:
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: name:
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: the line{"id":"2","name":"מהדקי אדמה"}
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: id:
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: name:
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: the line{"id":"3","name":"מכונות פוליש"}
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: id:
11-09 17:15:05.890 1580-1626/yuvallevy.powertoolsrental I/System.out: name:
有人能帮我理解如果我打印整行它是如何可能的,它很清楚每一行都有 "id" 并且还有 "name" 并且当我尝试只获取它们的值时我得到空结果?
这是 JSONArray:
[{"post":{"id":"0","name":"פטישי חציבה"}},{"post":{"id":"1","name":"מקדחות יהלום"}},{"post":{"id":"2","name":"מהדקי אדמה"}},{"post":{"id":"3","name":"מכונות פוליש"}}]
那是一个 post:
{"id":"0","name":"פטישי חציבה"}
您可能想这样做:
JSONObject curr = jsonArray.getJSONObject(i);
System.out.println("id: " + curr.optJSONObject("post").optString("id"));
System.out.println("name: " + curr.optJSONObject("post").optString("name"));