-1

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.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 2
    Is the question how to add items to an `ArrayList` or how to store the JSON notes as `Note` objects? – Eldar B. Mar 16 '20 at 17:39
  • Does this answer your question: [Gson - convert from Json to a typed ArrayList](https://stackoverflow.com/q/12384064)? – Pshemo Mar 16 '20 at 17:55
  • 1
    Does this answer your question? [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – alokraop Mar 16 '20 at 22:17

1 Answers1

1

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();
        }
ॐ Rakesh Kumar
  • 1,318
  • 1
  • 14
  • 24