【问题标题】:How to append new Array in JSON file stored in android如何在存储在android中的JSON文件中附加新数组
【发布时间】:2018-11-25 07:22:03
【问题描述】:

我的应用数据文件夹中有一个 JSON 文件。

 {
  "user": [
    {
      "identifier": "1",
      "name": "xyz",
      "contact": [
        {
          "contact": "123"
        },
        {
          "contact": "456"
        }
      ]
    }
  ]
}

现在我想像这样在运行时添加一个新用户。

{
  "user": [
    {
      "identifier": "1",
      "name": "xyz",
      "contact": [
        {
          "contact": "123"
        },
        {
          "contact": "456"
        }
      ]
    }
  ],
  "user": [
    {
      "identifier": "2",
      "name": "abc",
      "contact": [
        {
          "contact": "445"
        },
        {
          "contact": "789"
        }
      ]
    }
  ]
}

为此,我有这段代码,但它只是覆盖了现有用户。

String lastObject = getStringFronFile(new FileInputStream(file));
        try {
            JSONObject prevJSONObj = new JSONObject(lastObject);
            prevJSONObj.put("sticker_packs",newUserArray);
            writeJsonFile(file,prevJSONObj);
        }
        catch (Exception e) {
            Log.d("Json Error", e.toString());
        }

writeJsonFile是一种写入文件的方法。

任何帮助将不胜感激。

【问题讨论】:

    标签: android json android-studio


    【解决方案1】:

    你上面定义的结构是错误的,你不能在一个JSONObject中定义两个同名user的节点。

    如果您想将用户列表存储到文件中,可以这样存储:

    [
        {
            "name":"Jack",
            "age" :20
        },
        {
            "name":"Tom",
            "age" :20
        }
    ]
    

    对了,为什么不用SQLite数据库呢?

    【讨论】:

    • 感谢您提供宝贵的答复。我是 android 新手,所以我正在学习,将来肯定会使用 SQLite 数据库。
    【解决方案2】:

    Gson 简化了这项工作。添加gson依赖https://github.com/google/gson或者retrofit的gson(都一样) implementation 'com.squareup.retrofit2:converter-gson:2.4.0'。根据您的详细信息构建了以下 pojo 类:

    public class Example {
    @SerializedName("user")
    @Expose
    private List<User> user = null;
    
    public List<User> getUser() {
    return user;
    }
    
    public void setUser(List<User> user) {
    this.user = user;
    }
    }
    
    public class User {
    @SerializedName("identifier")
    @Expose
    private String identifier;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("contact")
    @Expose
    private List<Contact> contact = null;
    
    public String getIdentifier() {
    return identifier;
    }
    
    public void setIdentifier(String identifier) {
    this.identifier = identifier;
    }
    
    public String getName() {
    return name;
    }
    
    public void setName(String name) {
    this.name = name;
    }
    
    public List<Contact> getContact() {
    return contact;
    }
    
    public void setContact(List<Contact> contact) {
    this.contact = contact;
    }
    }
    
    public class Contact {
    @SerializedName("contact")
    @Expose
    private String contact;
    
    public String getContact() {
    return contact;
    }
    
    public void setContact(String contact) {
    this.contact = contact;
    }
    }
    

    您可以使用以下 sn-p 从字符串中获取详细信息并附加您的动态数据并更新文件。

    String lastObject = getStringFromFile(new FileInputStream(file));
    Example newJsonToReplace = new Example();
    List<User> usersList = new ArrayList();
    try{
        Type typeOfObject = new TypeToken<Example>() {
        }.getType(); // type you will be getting from string
        Example example = new Gson().fromJson(lastObject, typeOfObject); // converting string to pojo using gson
        if(example != null){
            usersList.addAll(example.getUser());
        }
        usersList.add(yourYourToAdd); // add your user object
        newJsonToReplace.setUser(usersList);
        String updatedList = new Gson().toJson(newJsonToReplace, typeOfObject); // again converting to json to string
        writeJsonFile(updatedList); // will replace the existing content
    }catch(Exception exception){
        Log.d("Json Error", e.toString());
    }
    

    【讨论】:

    • 感谢您的宝贵回答,它对我帮助很大。
    猜你喜欢
    • 2022-11-02
    • 1970-01-01
    • 2022-01-15
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 2021-11-10
    相关资源
    最近更新 更多