[英]How to return a from json converted array in java?
下面的代碼工作正常(!),我將其移至自己的函數中,但無法從json轉換后的Array返回(最后一行)。 我在做什么?
public Array jsonToArray(String json) {
JSONObject myjson = null;
try {
myjson = new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray the_json_array = null;
try {
the_json_array = myjson.getJSONArray("profiles");
} catch (JSONException e) {
e.printStackTrace();
}
int size = the_json_array.length();
ArrayList<JSONObject> arrays = new ArrayList<JSONObject>();
for (int i = 0; i < size; i++) {
JSONObject another_json_object = null;
try {
another_json_object = the_json_array.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
arrays.add(another_json_object);
}
JSONObject[] jsons = new JSONObject[arrays.size()];
arrays.toArray(jsons);
return Array jsons;
}
我認為問題是類型,但是在JAVA中是全新的...我收到錯誤:“不是聲明”。 含義和解決方案是什么?
public JSONObject[] jsonToArray()
以及
return jsons;
或者,為什么不返回ArrayList<JSONObject>
,為什么還要進行其他轉換呢?
不過,理想情況下,返回實際的JSONArray對象而不是JSONObject的Java數組更有意義。
如
return myjson.getJSONArray("profiles");
或者,更進一步,實際上是將所需的JSON值解析為自己的Java類嗎?
你應該做:
return jsons;
還要更正返回類型,您在其中有Array
,我沒有在代碼的任何位置看到該類型,並且在Java中也沒有這種類型,您可能需要JSONObject[]
,因此方法的第一行是:
public JSONObject[] jsonToArray(String json) {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.