Android Json解析详解
一、创建Json
1. 创建一级Json数据
//{"name":"LKing","age":20} try { JSONObject firstJson = new JSONObject(); firstJson.put("name","LKing"); firstJson.put("age",20); Log.e("LKing","json = "+firstJson.toString()); //结果:{"name":"LKing","age":20} } catch (JSONException e) { e.printStackTrace(); } |
2. 创建带数组Json数据
//{"name":"LKing","age":20,"phone":["123456","654321"]} try { JSONObject firstJson = new JSONObject(); firstJson.put("name","LKing"); firstJson.put("age",20); JSONArray firstJsonArray = new JSONArray(); firstJsonArray.put("123456").put("654321"); firstJson.put("phone",firstJsonArray); Log.e("LKing","json = "+firstJson.toString()); //结果:{"name":"LKing","age":20,"phone":["123456","654321"]} } catch (JSONException e) { e.printStackTrace(); }
|
3. 创建带数组、带二级Json数据
//{"name":"LKing","age":20,"phone":["123456","654321"],"address":{"country":"china","province":"shan'xi"}} JSONObject firstJson = new JSONObject(); try { firstJson.put("name","LKing"); firstJson.put("age",20); JSONArray firstJsonArray = new JSONArray(); firstJsonArray.put("123456").put("654321"); firstJson.put("phone",firstJsonArray); JSONObject second = new JSONObject(); second.put("country","china"); second.put("province","shan'xi"); firstJson.put("address",second); Log.e("LKing","json = "+firstJson.toString()); //结果:{"name":"LKing","age":20,"phone":["123456","654321"],"address":{"country":"china","province":"shan'xi"}} } catch (JSONException e) { //键为null或者使用json不支持的数字格式(NaN,infinities) e.printStackTrace(); }
|
4. 使用JSONStringer生成Json数据
try { JSONStringer firstJson = new JSONStringer(); //首先是{对象开始Object(),最后是以}对象结束endObject()。 // Object和endObject必须配对使用;array和endArray必须配对使用 firstJson.object(); firstJson.key("name"); firstJson.value("LKing"); firstJson.key("age"); firstJson.value(20); firstJson.endObject(); Log.e("LKing","firstJson = "+firstJson.toString()); //结果:{"name":"LKing","age":20} JSONStringer secondJson = new JSONStringer(); secondJson.object(); secondJson.key("phone"); secondJson.array(); secondJson.value("123456").value("654321"); secondJson.endArray(); secondJson.endObject(); Log.e("LKing","secondJson = "+secondJson.toString()); //结果:{"phone":["123456","654321"]} JSONStringer thirdJson = new JSONStringer(); thirdJson.object(); thirdJson.key("adress"); thirdJson.object(); thirdJson.key("country"); thirdJson.value("china"); thirdJson.key("province"); thirdJson.value("shan'xi"); thirdJson.endObject(); thirdJson.endObject(); Log.e("LKing","thirdJson = "+thirdJson.toString()); //结果:{"adress":{"country":"china","province":"shan'xi"}} } catch (JSONException e) { e.printStackTrace(); } |
二、解析Json:使用上面生成的firstJson
1. 使用JSONObject解析Json数据
try { JSONObject jsonObject = new JSONObject(firstJson.toString()); JSONArray jsonArray = jsonObject.getJSONArray("phone"); Log.e("LKing","jsonArray = "+jsonArray.toString()); //结果:["123456","654321"],这里也可以用for循环读取数据 int phoneOne = Integer.valueOf(jsonArray.get(0).toString()); int phoneTwo = Integer.valueOf(jsonArray.get(1).toString()); Log.e("LKing","phoneOne = "+ phoneOne); Log.e("LKing","phoneTwo = "+ phoneTwo); String name = jsonObject.getString("name"); Log.e("LKing","name = "+name); //结果:LKing int age = jsonObject.getInt("age"); Log.e("LKing","age = "+age); //结果:20 JSONObject object = jsonObject.getJSONObject("address"); Log.e("LKing","object = "+object); //结果:{"country":"china","province":"shan'xi"},这里再从新解析一遍Json数据 } catch (JSONException e) { e.printStackTrace(); } |
2. 使用JSONTokener解析Json数据
虽然是nextValue,但是也是根据key值来解析Json数据的。
try { JSONTokener jsonTokener = new JSONTokener(firstJson.toString()); JSONObject person = (JSONObject) jsonTokener.nextValue(); String name = person.getString("name"); Log.e("LKing","name = "+name); //结果:LKing JSONArray jsonArray = person.getJSONArray("phone"); Log.e("LKing","jsonArray = "+jsonArray.toString()); //结果:["123456","654321"],这里也可以用for循环读取数据 int phoneOne = Integer.valueOf(jsonArray.get(0).toString()); int phoneTwo = Integer.valueOf(jsonArray.get(1).toString()); Log.e("LKing","phoneOne = "+ phoneOne); Log.e("LKing","phoneTwo = "+ phoneTwo); int age = person.getInt("age"); Log.e("LKing","age = "+age); //结果:20 JSONObject object = person.getJSONObject("address"); Log.e("LKing","object = "+object); //结果:{"country":"china","province":"shan'xi"},这里再从新解析一遍Json数据 } catch (JSONException e) { e.printStackTrace(); } |