我想从服务器解析JSON响应,但我无法做到。请帮帮我
连接成功的服务器响应是
Database connection success....{"server_response":[{"code":"reg_true","0":"message=>Registration Success...Thank you....."}]}
我想解析它并在比较中使用server_response和代码值,所以我可以执行IF语句 当我调试我的代码。它显示json =数据库连接成功.... {“server_response”:[{“code”:“reg_true”,“0”:“message =>注册成功......谢谢......”} ]}
然后它跳转到catch(JSONException e) 请帮忙 这是我的代码
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonarry= jsonObject.getJSONArray("server_response");
JSONObject JO = jsonarry.getJSONObject(0);
String code = JO.getString("code");
String message = JO.getString("message");
if (code.equals("reg_true")) {
showDialog("Registration Success",code,message);
} else if (code.equals("reg_false")) {
showDialog("Registration Failed",code, message);
} else if (code.equals("login_true")) {
Intent intent = new Intent(activity, HomeActivity.class);
intent.putExtra("message", message);
activity.startActivity(intent);
} else if (code.equals("login_false")) {
showDialog("Login Error...",code,message);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public void showDialog(String title, String code, String message)
{
builder.setTitle(title);
if(code.equals("reg_true")||code.equals("reg_false"))
{
builder.setMessage(message);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
activity.finish();
}
});
答案 0 :(得分:2)
例外是因为特殊符号=>在你的json响应中。以有效格式发送有效的json字符串:
Database connection success....{"server_response":[{"code":"reg_true","0":"message Registration Success...Thank you....."}]}
你可以尝试一下。