我有一個模型類方法,它返回包含所有註冊用戶詳細信息的對象列表。我想獲取all()方法重新調用的列表,並將數據轉換爲JSON對象並將其作爲字符串傳遞給視圖。我怎樣才能將這個數組列表轉換爲JSON對象?將數組列表轉換爲json對象字符串
我無法通過以下要做到這一點:
ObjectMapper mapper = new ObjectMapper();
JSONObject json = new JSONObject();
JsonNodeFactory jsonnode = JsonNodeFactory.instance;
ObjectNode result = new ObjectNode(jsonnode);
for (int i = 0; i < list.size(); i++) {
json.put(list.get(i).fname, list.get(i));
System.out.println(json.get("fname"));
}
@Entity
class Mydata extends Model {
@Id
public Long Id;
public String fname;
public String lname;
public String city;
public String state;
/****************** READ/select OPERATION *****************/
public static Finder < Long, Mydata > finder = new Finder(Long.class, Mydata.class);
public static List <Mydata> all() {
return finder.all();
}
public static void createuser(Mydata user) {
user.save();
}
}
等待等待;你在這裏混合了Jackson和org.json! – fge
可能的複製http://stackoverflow.com/questions/5813434/trouble-with-gson-serializing-an-arraylist-of-pojos –
[數組列表轉換爲JSON對象字符串] [1] [1]:http://stackoverflow.com/questions/4841952/convert-arraylistmycustomclass-to-jsonarray/30193029#30193029 –