JSON数组无名称转换为Android Studio(Java)中的ArrayList:

huangapple go评论62阅读模式
英文:

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:

[{&quot;date&quot;:&quot;23.2.2004&quot;,&quot;note&quot;:&quot;Foo&quot;,&quot;title&quot;:&quot;Bar&quot;}, {&quot;date&quot;:&quot;23.2.2005&quot;,&quot;note&quot;:&quot;Foo1&quot;,&quot;title&quot;:&quot;Bar1&quot;}]

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+&quot;; &quot;+date+&quot;;&quot;+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&lt;Note&gt; arrlist = new ArrayList&lt;&gt;();

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 = &quot;[{\&quot;date\&quot;:\&quot;23.2.2004\&quot;,\&quot;note\&quot;:\&quot;Foo\&quot;,\&quot;title\&quot;:\&quot;Bar\&quot;}, {\&quot;date\&quot;:\&quot;23.2.2005\&quot;,\&quot;note\&quot;:\&quot;Foo1\&quot;,\&quot;title\&quot;:\&quot;Bar1\&quot;}]&quot;;
ArrayList&lt;Note&gt; noteList = new ArrayList&lt;&gt;();
try {
JSONArray jsonarray = new JSONArray(response);
Log.e(&quot;value&quot;,&quot;value---&quot;+jsonarray.length()+&quot;&lt;&lt;&gt;&gt;&quot;+jsonarray);
if(jsonarray.length()&gt;0)
{
for(int i = 0; i&lt;jsonarray.length(); i++)
{
JSONObject jsonObject = jsonarray.getJSONObject(i);
String date =  jsonObject.getString(&quot;date&quot;);
String note =  jsonObject.getString(&quot;note&quot;);
String title =  jsonObject.getString(&quot;title&quot;);
noteList.add(new Note(date,note,title));
}
}
if(noteList.size()&gt;0)
{
for(Note note :noteList)
{
Log.e(&quot;value&quot;,&quot;value--2--&quot;+note.note+&quot;&lt;&lt;date&gt;&gt;&quot;+note.date+&quot;&lt;&lt;title&gt;&gt;&quot;+note.title);
}
}
} catch (JSONException e) {
e.printStackTrace();
}

huangapple
  • 本文由 发表于 2020年3月17日 01:33:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/60710609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定