直接贴代码:(所需jar包:json-lib.jar,可能会关联一些其它的jar包,请自行搜索.)

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class JsonParser { @SuppressWarnings("rawtypes")
public void test() {
Map m = this.testJson("jsonString");
System.out.println(((Map) ((List) m.get("test")).get(0)).get("test_title"));
} @SuppressWarnings({ "rawtypes", "unchecked" })
public Map testJson(String str) {
JSONObject json = JSONObject.fromObject(str);
Iterator<?> it = json.keySet().iterator();
Map map = new HashMap();
while (it.hasNext()) {
String key = (String) it.next();
String value = json.getString(key);
if (this.isString(value)) {
map.put(key, value);
}
if (this.isJson(value)) {
map.put(key, this.testJson(value));
}
if (this.isJsonArray(value)) {
map.put(key, this.testJsonArray(value));
} }
return map;
} @SuppressWarnings({ "unchecked", "rawtypes" })
public List testJsonArray(String str) {
JSONArray jsonArr = JSONArray.fromObject(str);
List list = new ArrayList();
for (Object json : jsonArr) {
String jsonStr = json.toString();
    if(this.isString(jsonStr)){
    list.add(jsonStr);
    }
    if(this.isJson(jsonStr)){
    list.add(this.testJson(jsonStr.toString()));
    }
    if(this.isJsonArray(jsonStr)){
    list.add(this.testJsonArray(jsonStr.toString()));
    }
}
return list;
} public boolean isJson(String s) {
boolean flag = true;
try {
JSONObject.fromObject(s);
} catch (Exception e) {
flag = false;
}
return flag;
} public boolean isJsonArray(String s) {
boolean flag = true;
try {
JSONArray.fromObject(s);
} catch (Exception e) {
flag = false;
}
return flag;
} public boolean isString(String s) {
return !this.isJson(s) && !this.isJsonArray(s);
} public static void main(String[] args) {
JsonParser tj = new JsonParser();
tj.test(); } }

最新文章

  1. 各种Android手机Root方法
  2. 纯前端JSON文件编辑器[0]
  3. Free git private repo
  4. 使用mysql的长连接
  5. MySQL数据库中字符集的问题
  6. linux 下安装开发组件包
  7. c++对象模型以及内存布局的研究
  8. greensock的tween源码阅读笔记
  9. LINQ to SQL 语句(2)之 Select/Distinct
  10. IOS数据类型
  11. socket编程发送GET请求
  12. javascript 事件流及应用
  13. c++ 08
  14. JSP和JSTL
  15. UPS对电源故障的处理能力
  16. [笔记]Linux命令行大全
  17. JAVA之旅(十八)——基本数据类型的对象包装类,集合框架,数据结构,Collection,ArrayList,迭代器Iterator,List的使用
  18. 关于java集合类HashMap的理解
  19. linux 系统调用之文件操作
  20. django中多个app放入同一文件夹apps

热门文章

  1. Android -- DisplayMetrics
  2. 怎样将 MySQL 迁移到 MariaDB 上
  3. HTTP协议中源端口和目标端口的问题
  4. Provider Pattern for Beginners in .net
  5. Spring 集成 redistemplate
  6. ORA-16038: log 3 sequence# 103 cannot be archived
  7. LXC 容器集chroot使用说明
  8. Reshaper cannot resolve symbol
  9. 提高Mxd地图渲染出图效率的方法
  10. Word 之 清除页眉下划线