1

I'm trying to create a Kafka consumer in intellij but when I try to create the JsonParser it throws me an error :

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();
}

I have tried:

  1. gson maven dependency
  2. org.json.simple.JSONObject
  3. org.json.JSONObject

but the error still persist.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
DrGenius
  • 817
  • 1
  • 9
  • 26

1 Answers1

3

You need to create JsonParser using factory methods:

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(...);
Ori Marko
  • 56,308
  • 23
  • 131
  • 233