安卓笔记-如何解析服务器返回的Json数据

第一种方式解析服务器端传过来的Json数组
private List<CarInformation> jsonAnalytic(String result) {
    List<CarInformation> list=new ArrayList<>();
    if (result == null){
        Toast.makeText(getApplicationContext(), "您还未添加车辆信息,请添加后再来", Toast.LENGTH_SHORT).show();
    }else {
        try{
            JSONObject jsonObject = new JSONObject(result);

            JSONArray children = jsonObject.getJSONArray("carInformations");
            for (int j = 0; j < children.length(); j++) {
                jsonObject = children.getJSONObject(j);

                CarInformation carInformation = new CarInformation();

                carInformation.setCarInformationSerial(jsonObject.getString("carInformationSerial"));
                carInformation.setCarInformationBrand(jsonObject.getString("carInformationBrand"));
                carInformation.setCarInformationMake(jsonObject.getString("carInformationMake"));
                carInformation.setCarInformationModel(jsonObject.getString("carInformationModel"));
                list.add(carInformation);
                Log.d("WL", carInformation.getCarInformationBrand());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return null;
}
第二种方式,解析服务器传过来的Json对象,并且对对象进行判断取值

private void jsonAnalytic(String result) {
    try {
        JSONObject jsonObject = new JSONObject(result);

        String Status = jsonObject.getString("Status");
        if(Status.equals("ok")){
            String jsonObject2 = jsonObject.getString("user");
            if (jsonObject2.equals("false")) {
                Toast.makeText(this, "账号或者密码错误", Toast.LENGTH_SHORT).show();
            }else {
                jsonObject = jsonObject.getJSONObject("user");
                Oneself user = new Oneself();
                user.setOneselfIdCard(jsonObject.getString("oneselIdCard"));
                user.setOneselfIdName(jsonObject.getString("oneselIdName"));
                user.setOneselfPhone(jsonObject.getString("oneselfPhone"));
                user.setOneselfSerial(jsonObject.getString("oneselfSerial"));
                user.setOneselfName(jsonObject.getString("oneselfName"));

                //缓存用户ID
                SharedPreferences sp = getSharedPreferences("sp_demo", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("name",user.getOneselfSerial());
                editor.commit();

                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
            }
        }else if (Status.equals("error")){
            Toast.makeText(this, "账号或者密码错误", Toast.LENGTH_SHORT).show();
        }
        dismiss();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值