【问题标题】:Json edit object using Jackson(Java)Json 使用 Jackson(Java) 编辑对象
【发布时间】:2021-03-05 02:46:06
【问题描述】:

这里是杰克逊的新成员。 我在 Arraylist 中列出了 Person 类和 json 格式的人员列表。 Person 类包含以下内容:

int id;

@JsonProperty("Lastname")
String lastName;

@JsonProperty("Firstname")
String firstName;

@JsonProperty("BirthDay")
String birthDay;

@JsonProperty("Emails")
ArrayList<String> emails;

@JsonProperty("Languages")
ArrayList<String> languages;

public Person() {
}

public Person(int id, String lastName, String firstName, String birthDay, ArrayList<String> emails, ArrayList<String> languages) {
    this.id = id;
    this.lastName = lastName;
    this.firstName = firstName;
    this.birthDay = birthDay;
    this.emails = emails;
    this.languages = languages;
}

加上 getter、setter 和 toString。

我正在从 json 文件中读取所有对象,并将其添加到 ArrayList

static ArrayList<Person> personArrayList = new ArrayList<>();

搜索:

public static List<Person> searchByID(int id) {
    List<Person> search = new ArrayList();     
    for(Person person:personArrayList)
    {
        if(person.getId()== id)
        {
            search.add(person);
        }
    }      
    return search;
    }

问题是:当我在数组列表中找到一个对象时,我该如何编辑它?我需要更改一些字段并将对象写入 json 文件,但不是新的,只是编辑旧的。我该怎么做?

【问题讨论】:

  • 你能详细说明你的问题吗?

标签: java json jackson


【解决方案1】:

所以你的问题是关于如何在文件中编写优化的 json 数据。
看看这个问题的答案,它解释了在 Java 中写入文件的工作原理以及您的想法不适用于 Java 对文件的随机访问:
Use java to modify file contents in place

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    • 2017-04-19
    • 2020-03-18
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多