Java 8:如何编写lambda流以使用JsonArray? [英] Java 8: How to write lambda stream to work with JsonArray?

查看:10913
本文介绍了Java 8:如何编写lambda流以使用JsonArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java 8 lambdas和其他东西都很陌生......我想编写一个带有JsonArray的lambda函数,遍历它的JsonObjects并创建某个字段的值列表。



例如,一个接受JsonArray的函数:[{name:John},{name:David}]并返回[John列表,大卫]。



我写了以下代码:

  import org.json.simple.JSONArray; 
import org.json.simple.JSONObject;

公共类Main {
public static void main(String [] args){
JSONArray jsonArray = new JSONArray();
jsonArray.add(new JSONObject()。put(name,John));
jsonArray.add(new JSONObject()。put(name,David));
列表list =(List)jsonArray.stream()。map(json - > json.toString())。collect(Collectors.toList());
System.out.println(list);
}
}

但是,我收到错误:


线程main中的异常java.lang.NullPointerException




<你知道怎么解决吗?

解决方案

JSONArray java.util.ArrayList JSONObject 是<$ c $的子类c> java.util.HashMap 。



因此, new JSONObject()。put(name, John)返回与键关联的前一个值( null ),而不是 JSONObject 实例。因此, null 被添加到 JSONArray



<另一方面,这可行:

  JSONArray jsonArray = new JSONArray(); 
JSONObject j1 = new JSONObject();
j1.put(name,John);
JSONObject j2 = new JSONObject();
j2.put(name,David);
jsonArray.add(j1);
jsonArray.add(j2);
Stream< String> ss = jsonArray.stream()。map(json-> json.toString());
List< String> list = ss.collect(Collectors.toList());
System.out.println(list);

由于某种原因,我不得不将流管道分成两个步骤,因为否则编译器不会认识到 .collect(Collectors.toList())返回列表



输出为:

  [{name:John},{name: 大卫}] 


I'm very new to Java 8 lambdas and stuff... I want to write a lambda function that takes a JsonArray, goes over its JsonObjects and creates a list of values of certain field.

For example, a function that takes the JsonArray: [{name: "John"}, {name: "David"}] and returns a list of ["John", "David"].

I wrote the following code:

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class Main {
    public static void main(String[] args) {
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(new JSONObject().put("name", "John"));
        jsonArray.add(new JSONObject().put("name", "David"));
        List list = (List) jsonArray.stream().map(json -> json.toString()).collect(Collectors.toList());
        System.out.println(list);
    }
}

However, I get an error:

Exception in thread "main" java.lang.NullPointerException

DO you know how to resolve it?

解决方案

JSONArray is a sub-class of java.util.ArrayList and JSONObject is a sub-class of java.util.HashMap.

Therefore, new JSONObject().put("name", "John") returns the previous value associated with the key (null), not the JSONObject instance. As a result, null is added to the JSONArray.

This, on the other hand, works:

    JSONArray jsonArray = new JSONArray();
    JSONObject j1 = new JSONObject();
    j1.put ("name", "John");
    JSONObject j2 = new JSONObject();
    j2.put ("name", "David");
    jsonArray.add(j1);
    jsonArray.add(j2);
    Stream<String> ss = jsonArray.stream().map (json->json.toString ());
    List<String> list = ss.collect (Collectors.toList ());
    System.out.println(list);

For some reason I had to split the stream pipeline into two steps, because otherwise the compiler doesn't recognize that .collect (Collectors.toList()) returns a List.

The output is:

[{"name":"John"}, {"name":"David"}]

这篇关于Java 8:如何编写lambda流以使用JsonArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆