Java Is Json isValidJSON(final String json)

Here you can find the source of isValidJSON(final String json)

Description

check the string is a valid json string

License

Open Source License

Parameter

Parameter Description
json a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static boolean isValidJSON(final String json) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;

public class Main {
    /**/*from   w w w  .  j ava2s  . c  o m*/
     * check the string is a valid json string
     * @param json
     * @return
     * @throws IOException
     */
    public static boolean isValidJSON(final String json) throws IOException {
        boolean valid = true;
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.readTree(json);
        } catch (JsonProcessingException e) {
            valid = false;
        }
        return valid;
    }
}

Related

  1. isJSONValid(String jsonInString)
  2. isJSONValid(final String json)
  3. isJSONEqual(M expectedJSON, N actualJSON)
  4. isValidJSON(final String json)
  5. isValidJSON(final String json)