Android Json解析详解

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();
}

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值