我需要为以下帖子创建类似的JSON文件结构,但它需要是动态的, 当我搜索时,我发现了以下帖子,但该解决方案特定于条目1和条目2 我的需求是json的结构完全相同但可以获得任何实体名称,即实体名称可以是客户,地址,销售订单等而不是条目 然后使用键值等字段进行数组。在帖子中,字段在POJO'中被硬编码 但是我需要提供在条目中添加任何字段的能力......有一种方法可以提供动态解决方案吗?
谢谢!
Create JSON file with deep array
{
"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",
....
}
]
}
答案 0 :(得分: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;
}
}
我使用此问题的源代码编写了此示例:
答案 1 :(得分: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"));