1、举例字符串格式
String value = "[\n" +
" {\n" +
" \"type\": \"SEND\",\n" +
" \"dataMapping\": {\n" +
" \"userid\": \"account\",\n" +
" \"ts\": \"ts\",\n" +
" \"sign\": \"sign\",\n" +
" \"mobile\": \"taskItem.mobile\",\n" +
" \"msgcontent\": \"taskItem.content\",\n" +
" \"time\": \"taskItem.scheduleTime\",\n" +
" \"URL\": \"http://1.1.1.1:8081/api/sms/send\",\n" +
" \"password\": \"password\"\n" +
" },\n" +
" \"maxSendCount\": \"1000\",\n" +
" \"tps\": \"50\",\n" +
" \"channelPattern\": \"JSON\",\n" +
" \"sendSeparate\": \"\",\n" +
" \"httpHeaderParams\": {\n" +
" \"Authorization\": \"authorization\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"type\": \"REPORT\",\n" +
" \"dataMapping\": {\n" +
" \"userid\": \"account\",\n" +
" \"ts\": \"ts\",\n" +
" \"sign\": \"sign\",\n" +
" \"password\": \"password\",\n" +
" \"URL\": \"http://1.1.1.1/api/v2/sms/query\"\n" +
" },\n" +
" \"tps\": \"10\",\n" +
" \"channelPattern\": \"JSON\"\n" +
" }\n" +
"]";
2、解决字符串顺序改变
* value为要转换的字符串
* Feature.OrderedField:保持顺序不变
JSONObject parseObject = JSON.parseObject(value, Feature.OrderedField);
3、解决\n\t转义符消失,不能自动换行
SerializerFeature.PrettyFormat:格式化输出
parseObject.toString(SerializerFeature.PrettyFormat));