java解析任意层数json字符串的方法
//解析策略,有可能是解析json字符串,有可能为数据中的图片地址,email等
package cc.util.regex;
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 cc.util.regex;
import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import java.util.regex.matcher;
import java.util.regex.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 = "<img +?src=\"(.+?)\"";
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.
* <br><b>notice:</b> if return map you should better invoke map.get(null) to obtain value.
* @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}
*/
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 = pattern.compile(regular_json_item_name);
final matcher matcher = pattern.matcher(jsonstr);
final map<string, object> map = new hashmap<string, object>();
final jsonobject jsonobject = new jsonobject(jsonstr);
for ( ; matcher.find(); ) {
string groupname = matcher.group(1);
object obj = jsonobject.opt(groupname);
if (obj != null && isjsonarray(obj.tostring()))
matcher.region(matcher.end() + obj.tostring().replace("\\", "").length(), matcher.regionend());
if (obj != null && !map.containskey(groupname))
map.put(groupname, regexjson(obj.tostring()));
}
return map;
} else if (isjsonarray(jsonstr)) {
list<object> list = new arraylist<object>();
jsonarray jsonarray = new jsonarray(jsonstr);
for (int i = 0; i < jsonarray.length(); i++) {
object object = jsonarray.opt(i);
list.add(regexjson(object.tostring()));
}
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) {
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 = regexutil.regex(regexpolicy.json, jsonstr.substring(jsonstr.indexof("{"),
jsonstr.lastindexof("}")+1));
if (object instanceof string) {
log.e("string", object.tostring());
} else if (object instanceof map) {
@suppresswarnings("unchecked")
hashmap<string, object> map = (hashmap<string, object>)object;
iterator<entry<string, object>> iterator = map.entryset().iterator();
while (iterator.hasnext()) {
entry<string, object> entry = iterator.next();
if (entry.getvalue() instanceof list) {
log.e(entry.getkey(), entry.getvalue().tostring());
} else {
log.e(entry.getkey(), entry.getvalue().tostring());
}
}
} else if (object instanceof list) {
log.e("list", object.tostring());
}
上一篇: Java 反射机制的实例详解
下一篇: java中计算集合的交差并集示例代码