开发者社区> 问答> 正文

没有名称的JSON Array到Android Studio(Java)中的ArrayLis

我有这个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

展开
收起
montos 2020-03-22 10:46:07 1051 0
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();
            }
    

    回复来源:Stack Overflow

    2020-03-22 10:46:50
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载