Map中的Json对象,如何在java中获取它?

在 XPage 应用程序中,我在 sessionScope 变量中有 JSON 格式的应用程序配置。


我想知道如何在 Java 中再次获得这个配置?


我试过:


Map<String, Object> sesScope = ExtLibUtil.getSessionScope();

if (null != sesScope.get("configJSON")){

    JsonJavaObject jsonObject = new JsonJavaObject();

    jsonObject = (JsonJavaObject) sesScope.get("configJSON");

    System.out.println("got object?");

    System.out.println("json " + jsonObject.toString());

}   

在控制台中,我从来没有遇到过“得到对象?” 打印声明。我究竟做错了什么?


汪汪一只猫
浏览 193回答 3
3回答

慕容森

JsonJavaObject 是您的 POJO java 类吗?如果是,那么请使用 Fasterxml 的 ObjectMapper 将您的 JSON 数据映射到 JsonJavaObject 类的字段。使用此方法将您的 json 数据映射到任何 POJO 类public final T validateJson(final Map<String, Object> jsonMap, final T temmplateClass) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper objectMapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // convert JSON string to Map&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String jsonString = objectMapper.writeValueAsString(jsonMap);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return objectMapper.readValue(jsonString, (Class<T>) temmplateClass);&nbsp; &nbsp; &nbsp; &nbsp; } catch (JsonMappingException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NestableRuntimeException(String.format(ApiCommonConstants.INVALID_JSON_FIELD,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.getPath().get(e.getPath().size() - 1).getFieldName()));&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NestableRuntimeException(e.getMessage(), e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java