Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

java - How to Validate JSON with Jackson JSON

I am trying to use Jackson JSON take a string and determine if it is valid JSON. Can anyone suggest a code sample to use (Java)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Not sure what your use case for this is, but this should do it:

public boolean isValidJSON(final String json) {
   boolean valid = false;
   try {
      final JsonParser parser = new ObjectMapper().getJsonFactory()
            .createJsonParser(json);
      while (parser.nextToken() != null) {
      }
      valid = true;
   } catch (JsonParseException jpe) {
      jpe.printStackTrace();
   } catch (IOException ioe) {
      ioe.printStackTrace();
   }

   return valid;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...