如何在java中动态创建JSON?
我与getter和setter有一个客户关系示例客户关系是如何在java中动态创建JSON?,java,json,spring-boot,Java,Json,Spring Boot,我与getter和setter有一个客户关系示例客户关系是 customerEntity = { "customerNumber": "1234", "firstName": "Test", "email": "test@gmail.com", "id": "1", "middleName": &q
customerEntity =
{
"customerNumber": "1234",
"firstName": "Test",
"email": "test@gmail.com",
"id": "1",
"middleName": "doe",
"phone": "11111"
}
我有JsonProperty的java类属性和getter和setter,如下所示
Attributes =
{
"name": "string"
"value": "string"
}
我有一个包含Customerntiy随机元素的列表,例如:
List-stringlist={“firstName”,“phone”}
我已经创建了一个类型属性列表
List Attributeslist=newarraylist()代码>
我想创建stringlist中所有元素的Attributelist,例如:
Attributeslist =[
{
"name": "firstName"
"value": "Test"
},
{
"name": "phone"
"value": "11111"
}
]
为此,我编写了如下代码,但要在previewattributes.setValue()中传递什么因为该值取决于下面for循环中的mystring
。在本例中,它将是previewattributes.setValue(customerEntity.getFirstName())
和previewattributes.setValue(customerEntity.getPhone())代码>用于不同的迭代,但我如何编码
for (String mystring : stringlist) {
Attributes previewattributes;
previewattributes = new Attributes();
previewattributes.setName(mystring);
previewattributes.setValue(value here would be of customerentity);
Attributeslist.add(previewattributes);
}
客户身份:
@Entity
@Table(name = "Customer_tbl")
public class CustomerEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long rowId;
@Column(name = "customer_number")
private String customerNumber;
public String getCustomerNumber() {
return customerNumber;
}
public void setCustomerNumber(String customerNumber) {
this.customerNumber = customerNumber;
}
}
您可以在Java中使用反射。
更简单的方法是将customerEntity存储在HashMap中并从中提取。
HashMap<String,String> m=new HashMap<>();
m.put("customerNumber", customerEntity.getCustomerNumber());
m.put("firstName", customerEntity.getFirstName());
m.put("email", customerEntity.getEmail());
m.put("id", customerEntity.getId());
m.put("middleName", customerEntity.getMiddleName());
m.put("phone", customerEntity.getPhone());
您可以在Java中使用反射。
更简单的方法是将customerEntity存储在HashMap中并从中提取。
HashMap<String,String> m=new HashMap<>();
m.put("customerNumber", customerEntity.getCustomerNumber());
m.put("firstName", customerEntity.getFirstName());
m.put("email", customerEntity.getEmail());
m.put("id", customerEntity.getId());
m.put("middleName", customerEntity.getMiddleName());
m.put("phone", customerEntity.getPhone());
如果我理解正确,您有一个Customer Json数组,其中您希望将某些属性分隔开,请检查下面的代码
public static void main(String[] args) {
String jsonArray = "[\r\n" +
" {\r\n" +
" \"customerNumber\":\"1234\",\r\n" +
" \"firstName\":\"Test\",\r\n" +
" \"email\":\"test@gmail.com\",\r\n" +
" \"id\":\"1\",\r\n" +
" \"middleName\":\"doe\",\r\n" +
" \"phone\":\"11111\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"customerNumber\":\"1235\",\r\n" +
" \"firstName\":\"Test2\",\r\n" +
" \"email\":\"test2@gmail.com\",\r\n" +
" \"id\":\"2\",\r\n" +
" \"middleName\":\"doe2\",\r\n" +
" \"phone\":\"2222\"\r\n" +
" }\r\n" +
"]";
List<String> requiredKeys = Arrays.asList("firstName" , "phone");
JSONArray array = new JSONArray(jsonArray);
Iterator iterator = array.iterator();
JSONArray outputArray = new JSONArray();
while(iterator.hasNext()) {
JSONObject jsonObject = (JSONObject)iterator.next();
Iterator<String> keys = jsonObject.keys();
while(keys.hasNext()) {
String key = keys.next();
if(requiredKeys.contains(key)) {
JSONObject attribute = new JSONObject();
attribute.put("name", key);
attribute.put("value", jsonObject.get(key));
outputArray.put(attribute);
}
}
}
System.out.println(outputArray);
}
如果我理解正确,您有一个Customer Json数组,其中您希望将某些属性分隔开,请检查下面的代码
public static void main(String[] args) {
String jsonArray = "[\r\n" +
" {\r\n" +
" \"customerNumber\":\"1234\",\r\n" +
" \"firstName\":\"Test\",\r\n" +
" \"email\":\"test@gmail.com\",\r\n" +
" \"id\":\"1\",\r\n" +
" \"middleName\":\"doe\",\r\n" +
" \"phone\":\"11111\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"customerNumber\":\"1235\",\r\n" +
" \"firstName\":\"Test2\",\r\n" +
" \"email\":\"test2@gmail.com\",\r\n" +
" \"id\":\"2\",\r\n" +
" \"middleName\":\"doe2\",\r\n" +
" \"phone\":\"2222\"\r\n" +
" }\r\n" +
"]";
List<String> requiredKeys = Arrays.asList("firstName" , "phone");
JSONArray array = new JSONArray(jsonArray);
Iterator iterator = array.iterator();
JSONArray outputArray = new JSONArray();
while(iterator.hasNext()) {
JSONObject jsonObject = (JSONObject)iterator.next();
Iterator<String> keys = jsonObject.keys();
while(keys.hasNext()) {
String key = keys.next();
if(requiredKeys.contains(key)) {
JSONObject attribute = new JSONObject();
attribute.put("name", key);
attribute.put("value", jsonObject.get(key));
outputArray.put(attribute);
}
}
}
System.out.println(outputArray);
}
取决于您用于存储自定义项的数据结构。?您没有提到。使用反射,获取所有字段并对每个字段进行迭代。您有一个客户实体的JSON数组或一个列表?@Hades数组JSON@yatharthmeena贴出的答案是,你想要什么?取决于你用来存储你的客户资料的数据结构。?您没有提到。使用反射,获取所有字段并对每个字段进行迭代。您有一个客户实体的JSON数组或一个列表?@Hades数组JSON@yatharthmeena贴出的答案是你想要的?存储为hashmap有效,但我如何使用反射?存储为hashmap有效,但我如何使用反射?