import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONStringer;
import org.json.JSONTokener;
import cc.util.android.core.LogUtil;
import android.util.Log;
/**
* 解析多层次Json字符串,封装多层Json,避免字符串中有特殊字符而出现的错误
* @author wangcccong
* @version 1.140122
* create at: Fri, 13 Jun 2014
* update at: Mon, 1 Sep 2014
*/
public class JsonUtil {
private final static String regex = "\"([^\\\" ]+?)\":";
/**
* 一个方法解析多层json数据 json + 正则 + 递归
* @see {@link java.util.regex.Matcher}, {@link java.util.regex.Pattern}
* @param jsonStr
* @return {@link java.util.Map} or {@link java.util.List} or {@link java.lang.String}
*/
public static Object jsonParse(final String jsonStr) {
if (jsonStr == null) throw new NullPointerException("JsonString shouldn't be null");
try {
if (isJsonObject(jsonStr)) {
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(jsonStr);
final Map
map = new HashMap();
final JSONObject jsonObject = new JSONObject(jsonStr);
try {
for (; matcher.find(); ) {
String groupName = matcher.group(1);
Object obj = jsonObject.opt(groupName);
//Log.e(groupName, obj+"");
if (isJsonObject(obj+"") || isJsonArray(obj+"")) {
matcher.region(matcher.end() + (obj+"").replace("\\", "").length(), matcher.regionEnd());
map.put(groupName, jsonParse(obj+""));
} else {
map.put(groupName, obj+"");
}
}
} catch (Exception e) {
// TODO: handle exception
Log.e("object---error", e.getMessage()+"--"+e.getLocalizedMessage());
}
return map;
} else if (isJsonArray(jsonStr)) {
List