1

我需要创建与以下帖子类似的 JSON 文件结构,但它需要是动态的,当我搜索时,我发现了以下帖子,但解决方案特定于条目 1 和条目 2,我的需求是 json 的结构将完全是相同但可以获取任何实体名称,即实体名称可以是客户、地址、销售订单等,然后是包含键值等字段的数组。在帖子中,这些字段在 POJO 中是硬编码的,但我需要提供向条目中添加任何字段的能力...有一种方法可以提供动态解决方案吗?

谢谢!

使用深度数组创建 JSON 文件

{
    "customer": [
        {
            "id": "0001",
            "type": "USER",
            "f1": "USER",
            "f2": "USER1",
             ....
        },
        {
            "id": "0002",
            "type": "EMP",
            "property":"Salery",
            "f5": "USER",
            "f6": "USER1",
             ....
        }
    ],
    "Address": [
        {
            "id": "0005",
            "name": "Vacation",
            "property":"user",
        },
        {
            "id": "0008",
            "name": "Work",
            "f5": "USER",
            "f6": "USER1",
                 ....
        }
    ]
}
4

2 回答 2

2

您可以使用FieldNamingStrategy界面。它定义了 JSON 中属性名称和名称之间的映射。请看我的例子:

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.FieldNamingStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GsonProgram {

    public static void main(String... args) throws Exception {
        Entry entry1 = new Entry();
        entry1.setId(1);
        entry1.setType("USER");
        entry1.setProperty("Salary");
        Entry entry2 = new Entry();
        entry2.setId(2);
        entry2.setType("EMP");
        Entry entry3 = new Entry();
        entry3.setId(2);
        entry3.setType("EMP");
        entry3.setProperty("Work");
        Entry entry4 = new Entry();
        entry4.setId(2);
        entry4.setType("EMP");

        EntryListContainer entryListContainer = new EntryListContainer();
        ArrayList<Entry> entryList1 = new ArrayList<Entry>();
        ArrayList<Entry> entryList2 = new ArrayList<Entry>();

        entryList1.add(entry1);
        entryList1.add(entry2);
        entryList2.add(entry3);
        entryList2.add(entry4);

        entryListContainer.setEntryList1(entryList1);
        entryListContainer.setEntryList2(entryList2);

        Map<String, String> mapping = new HashMap<String, String>();
        mapping.put("entryList1", "customer");
        mapping.put("entryList2", "Address");

        Gson gson = new GsonBuilder().serializeNulls().setFieldNamingStrategy(new DynamicFieldNamingStrategy(mapping)).create();
        System.out.println(gson.toJson(entryListContainer));
    }
}

class DynamicFieldNamingStrategy implements FieldNamingStrategy {

    private Map<String, String> mapping;

    public DynamicFieldNamingStrategy(Map<String, String> mapping) {
        this.mapping = mapping;
    }

    @Override
    public String translateName(Field field) {
        String newName = mapping.get(field.getName());
        if (newName != null) {
            return newName;
        }

        return field.getName();
    }
}

class EntryListContainer {

    private List<Entry> entryList1;
    private List<Entry> entryList2;

    public void setEntryList1(List<Entry> entryList1) {
        this.entryList1 = entryList1;
    }

    public List<Entry> getEntryList1() {
        return entryList1;
    }

    public void setEntryList2(List<Entry> entryList2) {
        this.entryList2 = entryList2;
    }

    public List<Entry> getEntryList2() {
        return entryList2;
    }
}

class Entry {

    private int id;
    private String type;
    private String property;

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
    }

}

我使用这个问题的源代码编写了这个例子:

于 2013-06-06T09:22:10.640 回答
1

因此,您可以使用键作为实体/属性名称的映射(或嵌套映射)。然后将其转换为流

Map data = new HashMap();
List customerList = new ArrayList();
Map customer = new HashMap();
customer.put("id","00001");
customer.put("type","USER");
customerList.add(customer);

//can create new customer and add to list
data.put("customer",customerList);

ObjectMapper mapper = new ObjectMapper();
ByteArrayOutputStream out = new ByteArrayOutputStream();  

//can save to file instead of printing using mapper.writeValue(file,data)

mapper.writeValue(out, data);
System.out.println(out.toString("UTF-8"));
于 2013-06-06T09:20:00.920 回答