我有这个JSON文件:
[{"date":"23.2.2004","note":"Foo","title":"Bar"}, {"date":"23.2.2005","note":"Foo1","title":"Bar1"}]
我有这个课:
public class Note {
private String title;
private String note;
private String date;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getDate() {
return date;
}
@Override
public String toString() {
return note+"; "+date+";"+title;
}
public String StringDetail() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public Note(String title, String note, String date) {
this.title = title;
this.note = note;
this.date = date;
}
我想用这三个属性创建一个Arraylist条目。我有一个这样的Arraylist:
ArrayList<Note> arrlist = new ArrayList<>();
我想将数据从JSON保存到此arraylist。
问题来源:Stack Overflow
您可以执行以下操作:
String response = "[{\"date\":\"23.2.2004\",\"note\":\"Foo\",\"title\":\"Bar\"}, {\"date\":\"23.2.2005\",\"note\":\"Foo1\",\"title\":\"Bar1\"}]";
ArrayList<Note> noteList = new ArrayList<>();
try {
JSONArray jsonarray = new JSONArray(response);
Log.e("value","value---"+jsonarray.length()+"<<>>"+jsonarray);
if(jsonarray.length()>0)
{
for(int i = 0; i<jsonarray.length(); i++)
{
JSONObject jsonObject = jsonarray.getJSONObject(i);
String date = jsonObject.getString("date");
String note = jsonObject.getString("note");
String title = jsonObject.getString("title");
noteList.add(new Note(date,note,title));
}
}
if(noteList.size()>0)
{
for(Note note :noteList)
{
Log.e("value","value--2--"+note.note+"<<date>>"+note.date+"<<title>>"+note.title);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
回复来源:Stack Overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。