1. 简介
JAVAScript Object Notation是一种轻量级的数据交换格式具有良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持)。
JSON采用兼容性很高的文本格式,同时也具备类似于C语言体系的行为。JSON作为数据是目前网络中主流的数据传输格式之一,应用十分广泛,说是使用率达到99%一点也不勉强。
1.1. JSON支持的数据类型
JSON里面的数据是以一种键值对的方式存在 (“key”:“value”)中值的类型可以是下面数据类型中的任意一种:
(1)数字(整数或浮点数)
(2)逻辑值(true 或 false)
(3)字符串(在双引号中)
(4)数组(在方括号中)
(4)函数
(6)对象(在大括号中)
(7)null
1.2.JSON语法规则
JSON的语法规则非常的简单,就是使用大括号’{}’,中括号’[]’,逗号’,’,冒号’:’,双引号’“”’。嵌套对象、数组、字符串、数字、布尔值或空值。
2. json文件
2.1. 新建json文件
new-Folder-Assets Folder
2.1. 读取 json文件
读取 json文件分为assets文件夹读取、raw文件夹读取
2.1.1. 读取json文件工具类
public class JsonHelper {/*** 读取assets中的文件*/public static String readAssetsFile(Context context, String path) {String result = "";try {// read file content from fileStringBuilder sb = new StringBuilder("");InputStreamReader reader = new InputStreamReader(context.getResources().getAssets().open(path));BufferedReader br = new BufferedReader(reader);String str;while ((str = br.readLine()) != null) {sb.append(str);}result = sb.toString();br.close();reader.close();} catch (IOException e) {e.printStackTrace();}return result;}/*** 读取raw中的文件*/public static String readRawFile(Context context, int resourceId) {StringBuilder builder = new StringBuilder();BufferedReader reader = new BufferedReader(new InputStreamReader(context.getResources().openRawResource(resourceId)));try {for (String line = reader.readLine(); line != null; line = reader.readLine()) {builder.append(line).append("\n");}} catch (Exception e) {throw new RuntimeException(e);}return builder.toString();}
}
2.2. 读取assts json文件
/***assets获取json*/private void getAssetsJson() {String jsonStr = JsonHelper.readAssetsFile(this,"json/applyJson.json");Log.e("assets获取json:",jsonStr);}
2.2. 读取raw json文件
/*** raw获取json*/private void getRawJson() {String jsonStr = JsonHelper.readRawFile(this, R.raw.video_list);Log.e("raw获取json:",jsonStr);}
3. 解析json字符串
3.1. 数据源
private void initData() {parseObjectStr = "{\"msg\":\"操作成功\",\"code\":0,\"name\":\"网格员测试账号6\"," +"\"photo\":\"\",\"sessionId\":\"52312e93-c0d5-4a27-9425-2202212e178f\"" +",\"dept\":{\"searchValue\":null,\"createBy\":null,\"createTime\":null," +"\"updateBy\":null,\"updateTime\":null,\"remark\":null,\"params\":{}," +"\"deptId\":13850,\"parentId\":9449,\"gridId\":13850,\"ancestors\":\"" +"0,100,8720,9448,9449\",\"deptName\":\"潍坊市-高密市-密水街道-东关社区居委会" +"-东关社区居委会网格一\",\"orderNum\":\"0\",\"leader\":null,\"phone\":null," +"\"email\":null,\"status\":\"0\",\"delFlag\":null,\"parentName\":" +"\"潍坊市-高密市-密水街道-东关社区居委会\",\"deptCode\":null,\"deptGrade\"" +":\"4\",\"deptType\":null,\"isCgorg\":\"1\",\"lng\":null,\"lat\":null," +"\"address\":null,\"location\":null,\"component\":null,\"goNum\":null," +"\"gridHcount\":null,\"gridArea\":null,\"provinceId\":null,\"cityId\":null," +"\"countyId\":null,\"streetId\":null,\"communityId\":null,\"gridLevel\":null}}".replaceAll("\\/", "/");parseArrayStr = ("{\"hy\":[{\"acc\":\"0536010000067\",\"pas\":\"888888\"," +"\"wgyzh\":\"CS6\",\"region\":\"3707\"}],\"ip\":\"218.59.142.38\"}").replaceAll("\\/", "/");}
3.2. 解析object
/*** 解析Object*/private void parseObject() {try {JSONObject jsonObject = new JSONObject(parseObjectStr);if (Integer.parseInt(jsonObject.get("code").toString()) == 0) {String sessionId = jsonObject.getString("sessionId");Log.e(TAG, "=sessionId=" + sessionId);JSONObject dept = new JSONObject(jsonObject.getString("dept"));String deptName = dept.getString("deptName");Log.e(TAG, "=deptName=" + deptName);}} catch (JSONException e) {e.printStackTrace();}}
3.3. 解析array
/*** 解析Array*/private void parseArray() {try {JSONObject object = new JSONObject(parseArrayStr);String ip = object.getString("ip");JSONArray array = object.getJSONArray("hy");for (int i = 0; i < array.length(); i++) {JSONObject obj = (JSONObject) array.get(i);String wgname = obj.getString("wgyzh");Log.e(TAG, "=wgname=" + wgname);}} catch (JSONException e) {e.printStackTrace();}}
3.4. 封装object(一)
/*** @return* @desc 封装Object*/private String getPackageObject1() {JSONObject jsonObject = new JSONObject();try {jsonObject.put("userName", "userName");jsonObject.put("deptName", "deptName");jsonObject.put("deptNickName", "deptNickName");jsonObject.put("deptId", "deptId");jsonObject.put("parentId", "parentId");Log.e(TAG, "=jsonObject=" + jsonObject.toString());return jsonObject.toString();} catch (JSONException e) {e.printStackTrace();}return "";}}
3.4. 封装object(二)
/*** @return* @desc 封装Object*/private String getPackageObject2() {JSONObject jsonObject = new JSONObject();try {jsonObject.put("userName", "userName");jsonObject.put("deptName", "deptName");jsonObject.put("deptNickName", "deptNickName");jsonObject.put("deptId", "deptId");jsonObject.put("parentId", "parentId");JSONObject jsonObject2 = new JSONObject();jsonObject.put("fee", jsonObject2);jsonObject2.put("feeName", "费用名称");jsonObject2.put("feeValue", "费用值");Log.e(TAG, "=jsonObject=" + jsonObject.toString());return jsonObject.toString();} catch (JSONException e) {e.printStackTrace();}return "";}
3.5. 封装array
/*** @return* @desc 封装Array*/private String getPackageArray() {JSONObject jsonObject = new JSONObject();try {JSONArray jsonArray = new JSONArray();jsonObject.put("feelist", jsonArray);for (int i = 0; i < 4; i++) {JSONObject jsonObject2 = new JSONObject();jsonObject2.put("feeName", "费用名称" + i);jsonObject2.put("feeValue", "费用值" + i);jsonArray.put(jsonObject2);}Log.e(TAG, "=jsonObject=" + jsonObject.toString());return jsonObject.toString();} catch (JSONException e) {e.printStackTrace();}return "";}