Java用JSONObject-lib来解析json串
2014-09-25 12:40
363 查看
直接贴代码:(所需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(); } }
相关文章推荐
- 百度天气api开发 全解析xml和json格式 使用json-lib jsonObject、 Gson 方式把json 转换成 java对象
- 你必须掌握的Java基础:JSON解析工具-json-lib
- Java下json的解析方法-----GSON与JSONObject
- java后台服务器向手机端返回Json, 使用 "net.sf.json.JSONObject" 解析出现的异常问题
- Java构造和解析Json数据的两种方法(json-lib构造和解析Json数据, org.json构造和解析Json数据)
- android json解析java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1报错
- JSONObject 解析从网络获取的json数据时,java.lang.String cannot be converted to JSONObject解决办法(编码 UTF-8)
- java http 发请求,结果转jsonobject解析
- JSON解析之JSONObject对象---js对象转换成java对象
- Jetty+json-lib库抛异常的问题解决过程(java.lang.NoClassDefFoundError: net/sf/json/JSONObject)
- Java必备:JSON解析工具-json-lib
- new JSONObject(str)无法解析 报错:org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
- Java解析Json(org.json,json-lib)
- Java创建和解析Json数据方法(三)——json-lib包的使用
- json-lib:java产生json数据以及json数据在javascript中解析
- Json解析异常处理方式(JSONException: Value of type java.lang.String cannot be converted to JSONObject)
- JSONObject解析引发java.lang.OutOfMemoryError问题
- java json 的生成和解析 --json-lib
- 在www.json.org上公布了很多Java下的json解析工具,其中org.json和json-lib比较简单,两者使用上差不多
- Java创建和解析Json数据方法(四)——json-lib包的使用