第一种,没有双引号的key,它也是可以准确无误的通过各种json lib的转换,以及js的解析(当然js的对象的key本来就可以不用双引号包围起来。
[ [{name: "brandId1", type: "String1"},{name: "brandName1", type: "String1"}], [{name: "brandId2", type: "String2"},{name: "brandName2", type: "String2"}], [{name: "brandId3", type: "String3"},{name: "brandName3", type: "String3"}], [{name: "brandId4", type: "String4"},{name: "brandName4", type: "String4"}] 第二种,带有双引号的key,各种json lib一般转换出来的json,key都是由双引号包围起来的。 [ [{"name":"brandId1","type":"String1"},{"name":"brandName1","type":"String1"}], [{"name":"brandId2","type":"String2"},{"name":"brandName2","type":"String2"}], [{"name":"brandId3","type":"String3"},{"name":"brandName3","type":"String3"}], [{"name":"brandId4","type":"String4"},{"name":"brandName4","type":"String4"}] key 要用英文双引号包围起来,一直以来都觉得可以不用,因为key不使用双引号包围起来,能经过各种转换器的转换。 但是,今天用了几个json格式的校验工具,发现如果key没有双引号,打死都是校验不通过的。 有这么两个校验地址: http://www.jsonlint.com/ http://jsonformatter.curiousconcept.com/ 但是现在,打算对前台传进来的json格式进行校验,而不是在转换器进行转换的时候,遇到非法json格式的时候才抛出异常,而这些解析器基本上都要求key必须有双引号,那么之后我决定还是按照这个标准组织json数据。乖点比较好,省的弄出太多郁闷,除非自己写校验器,呵呵。 下面是一个偷偷从StringTree里面的JSONValidator偷出来的校验代码,去掉几行的抛出异常的代码,个人觉得没必要,我只要一个返回true或false证明json格式是否正确即可。 代码如下,挺长的210行,不看直接忽略: package test; import java.text.CharacterIterator; import java.text.StringCharacterIterator; public class JsonValidator { private CharacterIterator it; private char c; private int col; public JsonValidator() { } public boolean validate(String input) { input = input.trim(); boolean ret = valid(input); return ret; } private boolean valid(String 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 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 false; int 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 boolean ishex(char d) { return "0123456789abcdefABCDEF".indexOf(c) >= 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; } } |