bra bra
bra bra

Reputation: 33

Add new element in a file

mycode:

try {
        FileInputStream inputStream1 = getActivity().openFileInput("filename");
        BufferedReader b = new BufferedReader(new InputStreamReader(inputStream1));
        StringBuilder total = new StringBuilder();
        String line;
        while ((line = b.readLine()) != null) {total.append(line);}
        b.close();
        inputStream1.close();

    String json = total.toString();

    Items oldItems = new Gson().fromJson(json,Items.class);
    Items newItem = new Items();
    newItem.setName(example.getName());
    List<Items> items = new ArrayList<Items>();
    items.add(oldItems);
    items.add(newItem);
    Gson gson = new Gson();
    String itemsJson = gson.toJson(items);

    try {
        FileOutputStream outputStream =  getActivity().openFileOutput("filename", getActivity().MODE_PRIVATE);
        outputStream.write(itemsJson.getBytes());
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

} catch (Exception e) {
    e.printStackTrace();
    try {
        Items newItem = new Items();
        newItem.setItem(example.getName());
        Gson gson = new Gson();
        String json = gson.toJson(newItem);
        FileOutputStream outputStream =  getActivity().openFileOutput("filename", getActivity().MODE_PRIVATE);
        outputStream.write(json.getBytes());
        outputStream.close();
    } catch (Exception d) {
        d.printStackTrace();
    }
}

I'm trying to write a code that read and write json in a file. I have no problem writing this:

[{"name":"a"},{"name":"b"}]

but when i try to read it for add a new "item" i have an error. My idea is to add a new item when I click the button. I don't understand what is the problem and how i can fix this problem.

Items.java

class Items implements Serializable {
    @SerializedName("name")
    @Expose
    private String name;

public String getName() { return name;}

public void setName(String name) {
    this.name = name;
}

}

EDIT The error is this:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

Upvotes: 0

Views: 74

Answers (2)

strash
strash

Reputation: 1321

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

As you are using GSON PARSER

That works for me perfect-

String json = "[{\"name\":\"a\"},{\"name\":\"b\"}]";

        List<Items> writenItems =  new Gson()
                .fromJson(json, new TypeToken<List<Items>>(){}.getType());
                 writenItems
                .forEach($->
                 System.out.println("name: " + $.getName()));

Upvotes: 1

Aritra Chatterjee
Aritra Chatterjee

Reputation: 460

When you are writing your json to file, can you write it in this format ?

[{"name":"a"},{"name":"b"}], that is with double quotes surrounding the name key.

It should resolve this issue.

thanks

Upvotes: 0

Related Questions