简体   繁体   English

将包含JSON数组的JSONArray对象解析为JSON数组列表

[英]Parsing a JSONArray object, containing JSON arrays, to a list of JSON arrays

I am using a PrimeFaces JSONArray object containing JSON arrays, eg (simplified) 我正在使用包含JSON数组的PrimeFaces JSONArray对象, 例如 (简体)

[['james', 12, 2019], ['sarah', 29, 2015], ['robert', 15, 2011]]

as input to a static build method which runs over the fields of the nested arrays to construct a POJO representing their data. 作为静态构建方法的输入,该方法在嵌套数组的字段上运行以构建表示其数据的POJO。 I implement this using static build methods within both the entity and container-of-entity classes (the latter of which simply parses the JSON and calls the former build method to construct each entity). 我在实体和实体容器类中使用静态构建方法来实现此目的(后者通过简单地解析JSON并调用前一个构建方法来构建每个实体)。 This works fine with a crude for loop: 这对于粗糙的for循环可以很好地工作:

public static MyContainer buildContainer(JSONArray json) {
    MyContainer list = new MyContainer ();
    for (int i = 0; i < json.length(); i++) {
        MyEntity ent = MyEntity.buildEntity(json, i);
        list.add(ent);
    }
    return list;
}

but I would like a more elegant solution, in particular one using a functional approach (to improve my knowledge of functional Java, if for no better reason). 但是我想要一个更优雅的解决方案,特别是使用功能方法的解决方案(如果没有更好的理由,可以提高我对功能Java的了解)。 I use Jackson to parse the JSONArray representing my complete dataset to an array of JSONArrays ( ie a JSONArray[] ). 我使用Jackson将代表完整数据集的JSONArray解析为JSONArrays数组( JSONArray[] )。 This array can then be converted to a list, which allows easier functional manipulation. 然后可以将此数组转换为列表,从而可以更轻松地进行功能操作。 Here is my initial attempt at writing down what I think that would look like, which probably shows some fundamental misunderstandings of Jackson and functional Java: 这是我最初尝试写下我认为的样子,这可能表明对Jackson和功能性Java的一些基本误解:

public static MyContainer buildContainer(JSONArray json) {
    ObjectMapper mapper = new ObjectMapper();
    try {
        JSONArray[] jaArr = mapper.readValue(json.toString(), JSONArray[].class);
        List<JSONArray> jaList = Arrays.asList(jaArr);
        MyContainer list = (MyContainer) (List<MyEntity>) Arrays.asList(json)
                .stream()
                .map(MyEntity::buildEntity)
                .collect(Collectors.toList());
        return list;
    } catch (JsonProcessingException ex) {
        return null;
    }
}

Netbeans does not suggest anything is wrong with this, and there are no compile-time issues. Netbeans并不暗示这有什么问题,并且没有编译时问题。 At runtime, however, I get the exception 但是,在运行时,出现异常

Severe: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.primefaces.json.JSONArray out of START_ARRAY token.

So, my questions are why this exception arises, when the input is of the format I have shown above, and whether there is a more elegant solution than serialising the JSONArray to a string then reading it back as a JSONArray[] – ie could I convert my initial JSONArray more directly to a type that allows .stream() or .forEach() ? 因此,我的问题是为什么会出现这种异常,当输入具有上面显示的格式时,以及是否存在比将JSONArray序列化为字符串然后将其读回JSONArray []更好的解决方案– 我可以将我的初始JSONArray更直接地转换为允许.stream().forEach()

JSONArray in version 7.0 implements Iterable so you can do something like this: 7.0版中的JSONArray实现了Iterable因此您可以执行以下操作:

StreamSupport
  .stream(iterable.spliterator(), false)
  .map(MyEntity::buildEntity)
  .collect(Collectors.toList());

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM