对于重复出现的问题感到抱歉,但我在谷歌上找不到答案。 我试图将json解析为setter / getter类的对象。
public ArrayList<User> getArrayList(StringBuffer jsonReceived){
ArrayList<User> userArrayList = new ArrayList<User>();
JSONObject json = new JSONObject(jsonReceived.toString());
JSONArray userTable = json.getJSONArray("user");
for (int i = 0; i < userTable.length(); i++) {
User user = new User();
user.setId(userTable.getJSONObject(i).getInt("user_id"));
user.setLastName(userTable.getJSONObject(i).getString("user_last_name"));
user.setFirstName(userTable.getJSONObject(i).getString("user_first_name"));
user.setEmail(userTable.getJSONObject(i).getString("user_email"));
System.out.println(userTable.getJSONObject(i).getString("user_birthday"));
String dateStr = userTable.getJSONObject(i).getString("user_birthday");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date birthDate = sdf.parse(dateStr);
user.setBirthday(birthDate);
} catch (ParseException e) {
e.printStackTrace();
}
user.setCountry(userTable.getJSONObject(i).getString("user_country"));
user.setCity(userTable.getJSONObject(i).getString("user_city"));
userArrayList.add(user);
}
System.out.println("la list d'utilisateurs : "+userTable.toString());
System.out.println("ArrayList : "+userArrayList.toString());
return userArrayList;
}
这里是mon json string user:
{
"tag": "getUsers",
"success": 1,
"error": 0,
"user": [
{
"user_id": "1",
"user_last_name": "Alain",
"user_first_name": "Ron",
"user_birthday": "2014-02-01",
"user_email": "alain@ron.fr",
"user_country": "",
"user_city": ""
},
{
"user_id": "2",
"user_last_name": "Rodrigues",
"user_first_name": "Richard",
"user_birthday": null,
"user_email": "richard@r.fr",
"user_country": null,
"user_city": null
}
]}
我的错误日志是:
线程中的异常&#34; AWT-EventQueue-0&#34; org.json.JSONException:JSONObject [&#34; user_birthday&#34;]不是字符串。 at org.json.JSONObject.getString(JSONObject.java:661) at fr.moveo.applicationlourde.services.AbstractMethods.getArrayList(AbstractMethods.java:50) at fr.moveo.applicationlourde.Views.frames.WindowConnexion.actionPerformed(WindowConnexion.java:75) 在javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) 在javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2346) 在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
我已经尝试了在google的第一页和第二页中找到的所有方法,但它没有工作....