java递归解析json_java解析任意层数json字符串的⽅法//解析策略,有可能是解析json字符串,有可能为数据中的图⽚地址,email等
package ;
public enum RegexPolicy {
Json("Json"),
Image("ImageFromHtml");
private String value;
RegexPolicy (String value) {
this.value = value;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return value;
}
}
package ;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import Matcher;
import Pattern;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
/**
* A static Class help to Analyze data
* @author wangcccong
* @version 1.140122
* create at: 14-02-14
*/
public class RegexUtil {
/
/与解析策略相匹配的正则表达式
//private static final String REGULAR_IMG_HTML = "
private static final String REGULAR_JSON_ITEM_NAME = "\"([^\\\" ]+?)\":";
//private static final String REGULAR_JSON_ARRAY_NAME = ", *?\" *?([a-zA-Z0-9]*?) *?\" *?: *?\\[ *?\\{";
//公⽤⽅法解析,将字符串传⼊即可
public static Object regex(final RegexPolicy policy, final String data) {
switch (policy) {
case Json:
return regexJson(data);
case Image:
break;
default:
break;
}
return null;
}
/**
*      By recursively parse the Json string, obtain the Json string name by the regular expression,
* see also Matcher and Pattern and analysis of data. If the analytical data JsonObject object return key value pair (Map), *  if JsonArray List is returned, otherwise it returns String.
Notice: if return Map you should better (null) to obtain value.
*  Notice:
* @see {@link Matcher}, {@link Pattern}
* @param jsonStr
* @return {@link java.util.Map} or {@link java.util.List} or {@link java.lang.String}
*/
private static Object regexJson(final String jsonStr) {
if (jsonStr == null) throw new NullPointerException("JsonString shouldn't be null");
try {
if (isJsonObject(jsonStr)) {
final Pattern pattern = Patternpile(REGULAR_JSON_ITEM_NAME);
final Matcher matcher = pattern.matcher(jsonStr);
final Map map = new HashMap();
final JSONObject jsonObject = new JSONObject(jsonStr);
for ( ; matcher.find(); ) {
String groupName = up(1);
Object obj = jsonObject.opt(groupName);
if (obj != null && String()))
map.put(groupName, String()));
}
return map;
} else if (isJsonArray(jsonStr)) {
List list = new ArrayList();
JSONArray jsonArray = new JSONArray(jsonStr);
for (int i = 0; i < jsonArray.length(); i++) {
Object object = jsonArray.opt(i);
list.add(String()));
}
return list;
}
} catch (Exception e) {
// TODO: handle exception
Log.e("RegexUtil--regexJson", e.getMessage()+"");
}
return jsonStr;
}
/**
* To determine whether a string is JsonObject {@link org.json.JSONObject}
* @param jsonStr {@link java.lang.String}
* @return boolean
*/
private static boolean isJsonObject(final String jsonStr) {
java replace方法
if (jsonStr == null) return false;
try {
new JSONObject(jsonStr);
return true;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
/**
* To determine whether a string is JsonArray {@link org.json.JSONArray};
* @param jsonStr {@link java.lang.String}
* @return boolean
*/
private static boolean isJsonArray(final String jsonStr) {
if (jsonStr == null) return false;
try {
new JSONArray(jsonStr);
return true;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
//使⽤⽅法
Object object = (RegexPolicy.Json, jsonStr.substring(jsonStr.indexOf("{"), jsonStr.lastIndexOf("}")+1));
if (object instanceof String) {
Log.e("string", String());
} else if (object instanceof Map) {
@SuppressWarnings("unchecked")
HashMap map = (HashMap)object;
Iterator>  iterator = Set().iterator();
while (iterator.hasNext()) {
Entry entry = ();
if (Value() instanceof List) {
Log.Key(), Value().toString()); } else {
Log.Key(), Value().toString()); }
}
} else if (object instanceof List) {
Log.e("list", String());
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。