'JsonParser' 是抽象的;无法实例化 0

'JsonParser' is abstract; cannot be instantiated 0

我正在尝试在 intellij 中创建一个 Kafka 消费者,但是当我尝试创建 JsonParser 时它抛出了一个错误:

JsonParser is abstract; cannot be instantiated 0

private static JsonParser jsonParser = new JsonParser();

private static String extractIdFromTweet(String tweetJson){
   
    return jsonParser.parse(tweetJson)
            .getAsJsonObject()
            .get("id_str")
            .getAsString();
}

我试过:

  1. gson maven 依赖
  2. org.json.simple.JSONObject
  3. org.json.JSONObject

但错误仍然存​​在。

您需要使用工厂方法创建 JsonParser

The following example demonstrates how to create a parser from a string that contains an empty JSON array:

JsonParser parser = Json.createParser(new StringReader("[]"));

The class JsonParserFactory also contains methods to create JsonParser instances. JsonParserFactory is preferred when creating multiple parser instances. A sample usage is shown in the following example:

JsonParserFactory factory = Json.createParserFactory();  
JsonParser parser1 = factory.createParser(...);