您的位置:首页 > Web前端 > JavaScript

笔记 Json各种格式解析

2016-03-31 16:37 393 查看
=================================================================

Object

----------------------------

第一种:Object

{"abc":"guochaoalng"}

解析方法

JSONObject demojson = new JSONObject(参数) 获取总的json数据

String abc = demojson.getString("abc");

---------------------------------------------------------------

第二种 Object

{"one":"guo","two":"chaolang"}

解析方法:

JSONObject demojson = new JSONObject(参数) 获取总的json数据

String abc = demojson.getString("one");

String xyz = demo.json.getString("two");

=================================================================

Array

------------------------------

第一种 Array

{"num":[1,2,3]}

解析方法:

JSONObject demojson = new JSONObject(参数) 获取总的json数据

JSONArray abc = demojson.getJSONArray("num");

for(int i=0;i<abc.length();i++){

Int temp = abc.getInt(i);

}



{"num":["one","two","three"]}

解析方法:

JSONObject demojson = new JSONObject(参数) 获取总的json数据

JSONArray abc = demojson.getJSONArray("num");

for(int i=0;i<abc.length();i++){

String temp = abc.getString(i);

}

-----------------------------------------------------------------

第二种 Array

{"num":[[1],[2],[3]]} 数组中嵌数组

解析方法:

JSONObject demojson = new JSONObject(参数) 获取总的json数据

JSONArray abc = demojson.getJSONArray("num");

for(int i=0;i<abc.length();i++){

Int temp = abc.getJSONArray(i).getInt(0);

//abc.getJSONArray(i).getString(3);

}

注:这里是第二层数组中只有一个元素,如果有多个可能需要用到二重循环

================================================================================

Object和Array混合

------------------------------------

{"a":[{"aa":"11"},{"aa":"12"}]}

解析方法:

JSONObject demojson = new JSONObject(参数) 获取总的json数据

JSONArray abc = demojson.getJSONArray("a");

for(int i=0;i<abc.length();i++){

String temp = abc.getJSONArray(i).getString("aa");

}

================================================================================
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: