package pe.core.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JsonDecoder {
private String[] getSplit(String json, char leftSplit, char rightSplit) {
if (json != null && json.length() >= 2) {
char[] chars = json.toCharArray();
chars[0] = chars[chars.length - 1] = ' ';
for (int i = 0; i < chars.length - 2; i++) {
char f = chars[i];
if (i >= 1) {
if (((f == ',' && chars[i - 1] == leftSplit && chars[i + 1] == rightSplit)
|| (f == ',' && chars[i + 1] == rightSplit)
||(f==','&&chars[i-1]==']'&&chars[i+1]==rightSplit))
&&(true)) {
chars[i] = '#';
}
}
}
StringBuilder tmp=new StringBuilder(new String(chars));
int start=0;
if((start=tmp.indexOf(":[{"))!=-1){
String as=tmp.substring(start,tmp.indexOf("}]")+2);
as=as.replaceAll("#", ",");
tmp=tmp.replace(start, tmp.indexOf("}]")+2, as);
}
if((start=tmp.indexOf(":{"))!=-1){
int end=indexOf(tmp,"}",start,1);
String as=tmp.substring(start,end);
as=as.replace("#", ",");
tmp=tmp.replace(start, end, as);
}
return tmp.toString().trim().split("#");
}
return null;
}
private int indexOf(String org,String reg,int appearIndex){
if(org==null&®==null&&appearIndex<=0){
return (-1);
}
int found=0;
int count=0;
while((found=org.indexOf(reg))!=-1){
count++;
if(appearIndex==count)
break;
}
return found+count-1;
}
private int indexOf(StringBuilder org,String reg,int appearIndex){
return indexOf(org.toString(),reg,appearIndex);
}
private int indexOf(String org,String reg,int start,int appear){
if(org==null&®==null&&appear<=0){
return (-1);
}
String tmp=org.substring(start);
int found=0;
int count=0;
while((found=tmp.indexOf(reg))!=-1){
count++;
if(appear==count)
break;
}
return start+found+2;
}
private int indexOf(StringBuilder org,String reg,int start,int appear){
return indexOf(org.toString(),reg,start,appear);
}
private void replace(String org,String str,int start,int end){
if(org==null&&str==null){
return;
}
int length=org.length();
if(length<start||start<0){
return;
}
StringBuilder sb=new StringBuilder(org);
sb=sb.replace(start, end>length?length:end, str);
}
private String[] getMSplit(String json){
if(containsJsonArray(json)){
json=json.replaceFirst(":", "#");
return json.trim().split("#");
}else if(containsJsonObj(json)){
json=json.replaceFirst(":", "#");
return json.trim().split("#");
}
return json.split(":");
}
private boolean containsJsonArray(String json){
return json!=null&&json.contains("[{")&&json.contains("}]");
}
private boolean containsJsonObj(String json){
return json!=null&&json.contains(":{")&&json.contains("}");
}
public Map parseSingleObject(String json) {
Map m = new HashMap();
String[] s = this.getSplit(json, '"', '"');
if (s != null && s.length > 0) {
for (int i = 0; i < s.length; i++) {
String[] keys = this.getMSplit(s[i]);
String key=replace(keys[0]);
String value=keys[1].trim();
if(isJsonArray(value)){
List list=this.parseList(value);
m.put(key, list);
}else if(isJsonObject(value)){
Map d=this.parseSingleObject(value);
m.put(key, d);
}else{
m.put(key, replace(value));
}
}
}
return m;
}
private boolean isJsonArray(String array){
return array!=null&&array.startsWith("[")&&array.endsWith("]");
}
private boolean isJsonObject(String map){
return map!=null&&map.startsWith("{")&&map.endsWith("}");
}
public List<Map> parseList(String json) {
if (json.startsWith("[") && json.endsWith("]")) {
String[] items = this.getSplit(json, '}', '{');
List<Map> rs = null;
if (items != null && items.length > 0) {
rs = new ArrayList<Map>();
for (int i = 0; i < items.length; i++) {
rs.add(this.parseSingleObject(items[i]));
}
}
return rs;
}
return null;
}
private String replace(String src) {
return src.replaceAll("\"", "").trim();
}
public static void main(String[] args) {
JsonDecoder decoder = new JsonDecoder();
String json = "{\"name,h\":\"duck\",\"password\":\"123456\",\"password2\":123456,\"sex\":true}";
Map m=decoder.parseSingleObject(json);
System.out.println(m);
}
}
该结果为----->
{sex=true, password2=123456, password=123456, name,h=duck}
一个Map的键值对,能够支持更加复杂的JSON对象字符串到Map的转换,比如
{"user":{"name":"jake","age":32,"isPm":true},"books":[{"title":"老人与海","pagesCount":500},{"title":"宝岛骑兵","pagesCount":200}]}
针对这样的json字符串,结果解析为
一个Map,其中user的值为一个Map,books的值为一个List<Map>类型
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JsonDecoder {
private String[] getSplit(String json, char leftSplit, char rightSplit) {
if (json != null && json.length() >= 2) {
char[] chars = json.toCharArray();
chars[0] = chars[chars.length - 1] = ' ';
for (int i = 0; i < chars.length - 2; i++) {
char f = chars[i];
if (i >= 1) {
if (((f == ',' && chars[i - 1] == leftSplit && chars[i + 1] == rightSplit)
|| (f == ',' && chars[i + 1] == rightSplit)
||(f==','&&chars[i-1]==']'&&chars[i+1]==rightSplit))
&&(true)) {
chars[i] = '#';
}
}
}
StringBuilder tmp=new StringBuilder(new String(chars));
int start=0;
if((start=tmp.indexOf(":[{"))!=-1){
String as=tmp.substring(start,tmp.indexOf("}]")+2);
as=as.replaceAll("#", ",");
tmp=tmp.replace(start, tmp.indexOf("}]")+2, as);
}
if((start=tmp.indexOf(":{"))!=-1){
int end=indexOf(tmp,"}",start,1);
String as=tmp.substring(start,end);
as=as.replace("#", ",");
tmp=tmp.replace(start, end, as);
}
return tmp.toString().trim().split("#");
}
return null;
}
private int indexOf(String org,String reg,int appearIndex){
if(org==null&®==null&&appearIndex<=0){
return (-1);
}
int found=0;
int count=0;
while((found=org.indexOf(reg))!=-1){
count++;
if(appearIndex==count)
break;
}
return found+count-1;
}
private int indexOf(StringBuilder org,String reg,int appearIndex){
return indexOf(org.toString(),reg,appearIndex);
}
private int indexOf(String org,String reg,int start,int appear){
if(org==null&®==null&&appear<=0){
return (-1);
}
String tmp=org.substring(start);
int found=0;
int count=0;
while((found=tmp.indexOf(reg))!=-1){
count++;
if(appear==count)
break;
}
return start+found+2;
}
private int indexOf(StringBuilder org,String reg,int start,int appear){
return indexOf(org.toString(),reg,start,appear);
}
private void replace(String org,String str,int start,int end){
if(org==null&&str==null){
return;
}
int length=org.length();
if(length<start||start<0){
return;
}
StringBuilder sb=new StringBuilder(org);
sb=sb.replace(start, end>length?length:end, str);
}
private String[] getMSplit(String json){
if(containsJsonArray(json)){
json=json.replaceFirst(":", "#");
return json.trim().split("#");
}else if(containsJsonObj(json)){
json=json.replaceFirst(":", "#");
return json.trim().split("#");
}
return json.split(":");
}
private boolean containsJsonArray(String json){
return json!=null&&json.contains("[{")&&json.contains("}]");
}
private boolean containsJsonObj(String json){
return json!=null&&json.contains(":{")&&json.contains("}");
}
public Map parseSingleObject(String json) {
Map m = new HashMap();
String[] s = this.getSplit(json, '"', '"');
if (s != null && s.length > 0) {
for (int i = 0; i < s.length; i++) {
String[] keys = this.getMSplit(s[i]);
String key=replace(keys[0]);
String value=keys[1].trim();
if(isJsonArray(value)){
List list=this.parseList(value);
m.put(key, list);
}else if(isJsonObject(value)){
Map d=this.parseSingleObject(value);
m.put(key, d);
}else{
m.put(key, replace(value));
}
}
}
return m;
}
private boolean isJsonArray(String array){
return array!=null&&array.startsWith("[")&&array.endsWith("]");
}
private boolean isJsonObject(String map){
return map!=null&&map.startsWith("{")&&map.endsWith("}");
}
public List<Map> parseList(String json) {
if (json.startsWith("[") && json.endsWith("]")) {
String[] items = this.getSplit(json, '}', '{');
List<Map> rs = null;
if (items != null && items.length > 0) {
rs = new ArrayList<Map>();
for (int i = 0; i < items.length; i++) {
rs.add(this.parseSingleObject(items[i]));
}
}
return rs;
}
return null;
}
private String replace(String src) {
return src.replaceAll("\"", "").trim();
}
public static void main(String[] args) {
JsonDecoder decoder = new JsonDecoder();
String json = "{\"name,h\":\"duck\",\"password\":\"123456\",\"password2\":123456,\"sex\":true}";
Map m=decoder.parseSingleObject(json);
System.out.println(m);
}
}
该结果为----->
{sex=true, password2=123456, password=123456, name,h=duck}
一个Map的键值对,能够支持更加复杂的JSON对象字符串到Map的转换,比如
{"user":{"name":"jake","age":32,"isPm":true},"books":[{"title":"老人与海","pagesCount":500},{"title":"宝岛骑兵","pagesCount":200}]}
针对这样的json字符串,结果解析为
一个Map,其中user的值为一个Map,books的值为一个List<Map>类型