如何将对象的json数组对象转换为该对象的Java8可选列表 public可选GetCategory(AppUser foundUser)引发异常{ 可选; JSONParser=新的JSONParser(); org.json.simple.JSONObject json; //未加载惰性用户详细信息的修复程序 if(foundUser.getAppUserDetail()==null){ foundUser=appUserService.findByID(foundUser.getId()).OrelsThrow(()->new ModelNotFoundException(“无效用户”); } debug(“foundUser{}”,gson.toJson(foundUser.getAppUserDetail().getPhoneNumber()); 字符串输出=getCategoryServiceController.myGetCategory(); debug(“output{}”,output); json=(JSONObject)parser.parse(输出); ObjectMapper mapper=新的ObjectMapper(); GetCategoryResponseDto=新GetCategoryResponseDto(); dto=mapper.readValue((DataInput)json,GetCategoryResponseDto.class); 返回可选。of(dto);

如何将对象的json数组对象转换为该对象的Java8可选列表 public可选GetCategory(AppUser foundUser)引发异常{ 可选; JSONParser=新的JSONParser(); org.json.simple.JSONObject json; //未加载惰性用户详细信息的修复程序 if(foundUser.getAppUserDetail()==null){ foundUser=appUserService.findByID(foundUser.getId()).OrelsThrow(()->new ModelNotFoundException(“无效用户”); } debug(“foundUser{}”,gson.toJson(foundUser.getAppUserDetail().getPhoneNumber()); 字符串输出=getCategoryServiceController.myGetCategory(); debug(“output{}”,output); json=(JSONObject)parser.parse(输出); ObjectMapper mapper=新的ObjectMapper(); GetCategoryResponseDto=新GetCategoryResponseDto(); dto=mapper.readValue((DataInput)json,GetCategoryResponseDto.class); 返回可选。of(dto);,java,json,optional,Java,Json,Optional,这是更新后的代码,但这一行“GetCategoryResponseDto dto=mapper.readValue(json,GetCategoryResponseDto.class);”仍然会导致语法错误如果下面一行返回json格式的字符串,则不需要任何JSONParser String output = getCategoryServiceController.myGetCategory(); 具有将String和Class作为参数的readValue方法 public <T>

这是更新后的代码,但这一行“GetCategoryResponseDto dto=mapper.readValue(json,GetCategoryResponseDto.class);”仍然会导致语法错误

如果下面一行返回json格式的字符串,则不需要任何
JSONParser

String output = getCategoryServiceController.myGetCategory();
具有将
String
Class
作为参数的
readValue
方法

public <T> T readValue(String content,
          Class<T> valueType)
        throws IOException,
               JsonParseException,
               JsonMappingException
注意例如,
GetCategory
中有更多不必要的行

Optional<GetCategoryResponseDto> optional;
org.json.simple.JSONObject json = null;
可选;
org.json.simple.JSONObject json=null;

我还看到
mapper
为空,如果它为空,您可能会遇到
NullPointerException
如果它为空

Optional=null;
永远不要这样做。Optional类型的变量永远不应该为空。好的,我得到了,但是我希望以自定义
GetCategory的可选列表的形式输出响应到
对象…也不要将集合包装在可选的中。顺便说一下,方法的签名与您的要求不同。因此,不太清楚您要查找什么以及从何处查找。GetCategoryResponseDto=mapper.readValue(json,GetCategoryResponseDto.class);仍然给我语法错误,我需要帮助。这一行阻止我的程序运行错误消息是什么?它可能引发编译时错误您应该处理它们@MIikeEpsI再次更新代码并查看错误,至少这次它运行了,但我仍然得到运行时错误>>“org.json.simple.JSONObject不能转换为java.io.DataInput”我不知道你为什么要转换
(DataInput)json
?@MIikeEpsThanks这么多死区,现在可以工作了我使用了“dto=mapper.readValue(output,GetCategoryResponseDto.class);“@Deadpool你是最好的
Optional<GetCategoryResponseDto> optional;
org.json.simple.JSONObject json = null;