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.

0

1 Answer 1

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(...); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.