猿问

在 Json 中保留几个字段 - Java

public class User {

String name;

String id;

Address[] address;

....

....

//40 more fields

}


public class Address {

String street;

String city;

String state;


}

我有一个列表,我需要将它转换为只有几个字段的 json。


public String fetchUsers(List<Users> users, List<String> fields) {

//fetch the list of users having the specific fields in the list and return as json

}

字段 = [“名称”,“地址.状态”]


我可以删除 json 中的字段......但是,我需要根据方法中传递的值保留受限制的字段。我也可以使用任何第三方库。


烙印99
浏览 358回答 1
1回答

largeQ

将您的对象的com.google.gson.Gson库使用serialize到 json 中,您必须ExclusionStrategy为您的对象创建要跳过或不跳过的字段。GsonBuilder从中创建一个对象并从中解析您的对象。这是完美的工作正常。import com.google.gson.ExclusionStrategy;import com.google.gson.FieldAttributes;import com.google.gson.Gson;import com.google.gson.GsonBuilder;import java.util.ArrayList;import java.util.Arrays;import java.util.List;&nbsp;public class ABC {&nbsp; public class User {&nbsp; &nbsp; public String name;&nbsp; &nbsp; public String id;&nbsp; &nbsp; public Address[] address;&nbsp; }&nbsp; public class Address {&nbsp; &nbsp; public String street;&nbsp; &nbsp; public String city;&nbsp; &nbsp; public String state;&nbsp; }&nbsp; public static ExclusionStrategy createStrategy(List<String> fields) {&nbsp; &nbsp; return new ExclusionStrategy() {&nbsp; &nbsp; &nbsp; public boolean shouldSkipField(FieldAttributes fieldAttributes) {&nbsp; &nbsp; &nbsp; &nbsp; if (fields.stream().anyMatch(e -> e.equals(fieldAttributes.getName()))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; public boolean shouldSkipClass(Class aClass) {&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; }&nbsp; public String fetchUsers(List<User> users, List<String> fields) {&nbsp; &nbsp; GsonBuilder builder = new GsonBuilder();&nbsp; &nbsp; builder.setExclusionStrategies(createStrategy(fields));&nbsp; &nbsp; Gson gson = builder.create();&nbsp; &nbsp; return gson.toJson(users);&nbsp; }&nbsp; public static void main(String[] args) {&nbsp; &nbsp; ABC x = new ABC();&nbsp; &nbsp; Address add = x.new Address();&nbsp; &nbsp; add.city = "city";&nbsp; &nbsp; add.state = "state";&nbsp; &nbsp; add.street = "street";&nbsp; &nbsp; Address[] array = new Address[1];&nbsp; &nbsp; array[0] = add;&nbsp; &nbsp; User user = x.new User();&nbsp; &nbsp; user.address = array;&nbsp; &nbsp; user.id = "id";&nbsp; &nbsp; user.name = "name";&nbsp; &nbsp; List<User> users = new ArrayList<>();&nbsp; &nbsp; users.add(user);&nbsp; &nbsp; List<String> fields = Arrays.asList("name", "address", "state");&nbsp; &nbsp; String json = x.fetchUsers(users, fields);&nbsp; &nbsp; System.out.println(json);&nbsp; }}此代码的输出是:[{"name":"name","address":[{"state":"state"}]}]和 Gson 的依赖是。<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; <groupId>com.google.code.gson</groupId>&nbsp; &nbsp; &nbsp; <artifactId>gson</artifactId>&nbsp; &nbsp; &nbsp; <version>2.8.5</version>&nbsp; &nbsp; </dependency>
随时随地看视频慕课网APP

相关分类

Java
我要回答