我想在String JsonObject中获取值

时间:2018-06-12 04:05:21

标签: android arrays json

    try {
        JSONObject jobj = new JSONObject(response);
        String status = jobj.getString("status");
        String error = jobj.getString("result");

        if(status == "1"){
            builder.setTitle("Server Message");
            builder.setMessage("Please validate your ID in registered email.");

        }else if(status == "0")
        {
            builder.setTitle("Server Message");
            builder.setMessage(error);
        }else{
            builder.setTitle("Server Message");
            builder.setMessage(response);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent mainIntent = new Intent(RegisterActivity.this, LoginActivity.class);
            startActivity(mainIntent);
        }
    });

    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}

我的回答是{"状态":" 1","结果":" true") 但是我无法做到" status"意味着我无法获得" 1"在状态内。

3 个答案:

答案 0 :(得分:3)

两个错误

  1. 将整数和布尔值作为字符串
  2. 将字符串与==
  3. 进行比较

    这样做

    int status = jobj.getInt("status");
    Boolean error = jobj.getBoolean("result");
    
    if(status == 1){
        builder.setTitle("Server Message");
        builder.setMessage("Please validate your ID in registered email.");
    }else if(status == 0) 
    {
        ...
        ...
    

答案 1 :(得分:1)

我希望这对你有用。

使用String

 String status = jobj.getString("status");
    String error = jobj.getString("result");

   if(status.equalsIgnoreCase("1")){
      builder.setTitle("Server Message");
     builder.setMessage("Please validate your ID in registered email.");

    }else if(status.equalsIgnoreCase("0")){
        // your other code.
}

答案 2 :(得分:0)

" =="是参考比较。 所以,替换" =="等于()

status.equals("1")