Recently encountered in the project JSON format validation problem, because the request interface may have the old and new version compatibility issues, the old version of the client, the situation is not said, bad, various formats have, look like JSON, but ... Oh.
So it is necessary to do compatibility, you have to standardize things before, and verify their correctness; the tool class is as follows:
/** * project name:v3a-b2c * file name:jsonvalidator.java * package name:com.v3a.util * date:2014 October 26 pm 1:59:59 * copyright (c) 2014, [ email protected] all rights reserved. **/package com.v3a.util;import Java.text.characteriterator;import java.text.stringcharacteriterator;import java.util.list;import java.util.regex.Pattern;import org.apache.commons.lang.StringUtils;import Com.google.common.collect.lists;/** * classname:jsonvalidator <br/> * function: is used to verify whether a string is a valid JSON format. <br/> * date: 2014 October 26 Afternoon 1:59:59 <br/> * Usage: new jsonvalidator (). Validate ("Josn string") * @author jinhui * @version * @since JDK 1.7 * @see */public class jsonvalidator {private characteriterator it;private Char c;private int col;public jsonvalidator () {}/** * Verify that a string is a valid JSON string * @param input The string to validate * @return true-legal ,false-Illegal */public boolean validate (String input) {input = input.trim (); boolean ret = valid (input); return ret;} Private boolean valid (string input) {/** * compatible with non-canonical JSON format (using single quotes), replace single quotation marks with double quotes */input =formartjson (input);if ("". Equals (input)) return true;boolean ret = true;it = new stringcharacteriterator (input); C = it.first ();col = 1; if (!value ()) {ret = error ("value", 1); else {skipwhitespace ();if (C != characteriterator.done) {ret = error ( "End", col);}} Return ret;} PrivatE boolean value () {return literal ("true") | | literal ("false") | | literal ("null") | | string () | | number () | | object () | | array ();} Private boolean literal (String text) {CharacterIterator ci = new StringCharacterIterator (text); Char t = ci.first ();if (c != t) return false; int start = col;boolean ret = true;for (T = ci.next (); t != characteriterator.done; t = ci.next ()) {if (t != Nextcharacter ()) {ret = false;break;}} Nextcharacter ();if (!ret) error ("literal " + text, start); return ret;} Private boolean array () {return aggregate (' [', '] ', false);} Private boolean object () {return aggregate (' {', '} ', true);} Private boolean aggregate (char&Nbsp;entrycharacter, char exitcharacter, boolean prefix) {if (c != Entrycharacter) Return false;nextcharacter (); Skipwhitespace ();if (c == exitcharacter) {Nextcharacter (); return true;} for (;;) {if (prefix) {int start = col;if (!string ()) Return error ("string", start); Skipwhitespace ();if (c != ': ') return error ("Colon", col); NextCharacter () ; Skipwhitespace ();} if (Value ()) {skipwhitespace ();if (c == ', ') {nextcharacter ();} else if (C == exitcharacter) {break;} else {return error ("comma or " + exitcharacter, col);} else {return error ("value", col);} Skipwhitespace ();} Nextcharacter (); return true;} Private boolean number () {if (! Character.isdigit (c) && c != '-') RETURN&NBSP;FALSE;INT&Nbsp;start = col;if (c == '-') Nextcharacter ();if (c == ' 0 ') { Nextcharacter ();} else if (Character.isdigit (c)) {while (Character.isdigit (c)) Nextcharacter (); else {return error ("number", start);} if (c == '. ') {nextcharacter ();if (Character.isdigit (c)) {while (Character.isdigit (c)) Nextcharacter (); else {return error ("number", start);}} if (c == ' e ' | | c == ' E ') {nextcharacter ();if (c == ' + ' | | c == '-') {nextcharacter ();} if (Character.isdigit (c)) {while (Character.isdigit (c)) Nextcharacter (); else {return error ("number", start);}} Return true;} Private boolean string () {if (c != ' ") return false;int start = col;boolean escaped = false;for (Nextcharacter (); C != characteriterator.done; nextcharacter ()) {if (!escaped && c == ' \ \ ') {escaped = true;} else if (escaped) {if (!escape ()) {return false;} Escaped = false;} else if (c == ' "') {nextcharacter (); return true;}} Return error ("quoted string", start);} Private boolean escape () {int start = col - 1;if (" \\\"/ Bfnrtu ". IndexOf (c) < 0) {return error (" escape sequence \\\ ", \\\\,\\/,\\ b,\\f,\\n,\\r,\\t or \\uxxxx ", start);} if (c == ' u ') {if (!ishex (Nextcharacter ()) | | !ishex (Nextcharacter ()) | | !ishex (Nextcharacter ()) | | !ishex (Nextcharacter ())) {return error ("Unicode escape sequence \\uxxxx ", start);}} Return true;} Private&nBsp;boolean ishex (char d) {return "0123456789abcdefABCDEF". IndexOf (d) >= 0;} Private char nextcharacter () {c = it.next (); ++col;return c;} Private void skipwhitespace () {while (Character.iswhitespace (c)) {nextcharacter ();}} Private boolean error (String type, int col) {system.out.printf ("type: %s, col: %s%s ", type, col, system.getproperty (" Line.separator ")); return false;} Public static void main (String[] args) {string json = "{firstName : Brett, ' lastName ': ' McLaughlin ', ' email ': ' aaaa '}; boolean valid = new jsonvalidator (). valid (JSON); SYSTEM.OUT.PRINTLN (valid);} Public static string formartjson (String json) {string replaceall = json.trim (). ReplaceAll ("'", "\" "). Replace (" ", " ");char[] chararray = replaceall.tochararray (); string regex = "^[a-za-z]+$"; Pattern pat = pattern.compile (REGEX); List<integer> list = lists.newlinkedlist (); Stringbuilder builder = new stringbuilder (ReplaceAll);for (int i = 0; i < chararray.length-1; i++) {if (i < chararray.length) { String current = string.valueof (Chararray[i]). Trim (); if (Stringutils.isempty (Current.trim ())) { Builder.deletecharat (i); continue;} Else{boolean flagcurrent = pat.matcher (current). find (); String last = string.valueof (chararray[i + 1]). Trim (); boolean flaglast = pat.matcher (last). Find ();/* * if the current punctuation is false, if the letter is true, the next (last) is the letter, then continue, if it is punctuation, check whether it is double quotes, * do not note the flag bit */if (flagcurrent) {if (!flaglast) {if (!check (last)) List.add (i+1);}} else{/* * if the current is not a punctuation, check that the next (last) is notIs the string, if it is, the current punctuation is not double quotation marks, not, then the record position * if it is not currently a string and last is a string, check that the current is not a quotation mark * */if (! Flagcurrent&&flaglast) {if (!check (current)) {list.add (i+1);}}}} Stringbuilder builder = new stringbuilder (ReplaceAll);for (int i = 0 ; i < list.size (); i++) {builder.insert (List.get (i) +i, "\" ");} Return builder.tostring ();} /** * check characters are not standard punctuation in JSON * check * @author brilliance * @param checkstring * @return * @since jdk 1.7 */public static boolean check (string checkstring) {boolean flag = true;switch (checkstring) { case "{":flag = false;break;case "}":flag = false;break;case "[":flag = false;break;case ",":flag = false;break;case ":": Flag = false;break;case "\" ": Flag = true;break;} ReturN flag;}}
JSON formatting and JSON validation tools