我有一個具有整數和字符串值的多維數組。我想以json格式轉換數組並將其發送回ajax函數。我試圖打印數組內容來檢查,但我無法這樣做。將數組轉換爲json
Os[] o1 = new os[];
o1[0].os_name = "xyz";
o1[0].os_id = 1;
JSONArray jsArray = new JSONArray(o1);
for (int i = 0; i < jsArray.length(); ++i) {
JSONObject rec = jsArray.getJSONObject(i);
int id = rec.getInt("os_id");
String loc = rec.getString("os_name");
System.out.println(id+loc);
}
我有一個OS分類:
public class Os {
int os_id;
String os_name;
}
我得到一個錯誤:
JSONObject["os_id"] not found.
當使陣列將幫助,也許就像聲明大小: '輸出[] 01 =新的Os [1];' –
您的第一行已損壞。 Os [] o1 = new os [];第二個'os'不是指一個對象,並且該數組沒有被聲明爲一個大小。 – Creperum
'o1 [0] .os_name =「xyz」;'不應該工作,因爲1)您的數組沒有大小。並且2)數組中沒有對象 –