简体   繁体   中英

how to parse JSON response from server and use it in comparison

I want to parse JSON response from server but I m unable to do it. please help me

server response for connection success is

Database connection success....{"server_response":[{"code":"reg_true","0":"message=>Registration Success...Thank you....."}]}

I want to parse it and use server_response and code values in comparing so I can execute IF statement When I debug my code. It shows that json = Database connection success....{"server_response":[{"code":"reg_true","0":"message=>Registration Success...Thank you....."}]}

but then it jump to catch (JSONException e) please help this is my code

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

           });

The exception is because of the special symbol => inside your json response. Send a valid json string in valid format as:

Database connection success....{"server_response":[{"code":"reg_true","0":"message Registration Success...Thank you....."}]}

You can try it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM