英文:
JSON Array without name to ArrayLis in Android Studio(Java)
问题
我有这个 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 中。
英文:
I have this JSON file:
[{"date":"23.2.2004","note":"Foo","title":"Bar"}, {"date":"23.2.2005","note":"Foo1","title":"Bar1"}]
I have this class:
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;
}
And I want to make an Arraylist entry with this three attributes.
I have an Arraylist like this:
ArrayList<Note> arrlist = new ArrayList<>();
and I want to save the data from JSON to this arraylist.
答案1
得分: 1
以下是翻译好的代码部分:
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();
}
英文:
You can do like the following:
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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论