如何解析以下JSON的应用程序

时间:2016-06-21 06:20:50

标签: android json

我是JSON解析的新手,我正在使用这个json

{"data":{"id":171,"public_id":"RNT-POMO5","sales_user_public_id":"","email":"gayatrid@webwingtechnologies.com","mobile_no":"2222222222","role":"sales","password":"$2y$10$Z0TQQ.EOEBqP7AkIJo429OJ4ASo.0lP5kzlj22Sejz0L7S7WE7bp6","permissions":[],"last_login":"2016-06-21 05:16:50","profile_pic":"http:\/\/sell4cell.com\/justdial_dev\/public\/uploads\/users\/profile_pic\/1d567217c228ea454a046bc7c8906ecd0b0fb567.jpg","first_name":"Gayatri","prefix_name":"0","middle_name":null,"last_name":"dd","gender":"female","d_o_b":"1970-01-01","dd":"","mm":"","yy":"","marital_status":"Married","city":"411","area":"Nashik","occupation":"","work_experience":"","street_address":"Nashik","mobile_OTP":"","home_landline":null,"std_home_landline":"","office_landline":"1234567890","std_office_landline":"","extn_office_landline":"","pincode":"422003","is_active":"1","via_social":"1","ask_for_old_password":"1","deleted_at":null,"created_at":"2016-05-02 05:29:47","updated_at":"2016-06-21 05:38:58","company_name":"","title":"","married_date":"1970-01-01","country":"1","state":"21"},"status":"SUCCESS","message":"Login successfully !"}

这是JSON对象,我正在解析我需要从登录到主要活动,但问题是我无法从一个切换到另一个活动。所以我需要匹配“消息”,如果它成功需要切换MainActivity。

到目前为止我做了什么

    private void login(final String email, final String password) {

        class LoginAsync extends AsyncTask<String,String,JSONObject>{

            JSONParser mJSONParser = new JSONParser();
            private static final String LOGIN_URL = "http://sell4cell.com/justdial_dev/public/api/login";

            private static final String TAG_MESSAGE = "data";
           // private static final String TAG_STATUS = "success";

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setMessage("Loading....");
                progressDialog.show();
            }

            @Override
            protected JSONObject doInBackground(String... args) {

                try{
                    HashMap<String,String> params = new HashMap<>();

                    params.put("email",email);
                    params.put("password",password);

                    Log.d("doInBackground: ","starting");

                    JSONObject json = mJSONParser.makeHttpRequest(LOGIN_URL,"POST",params);
                    return json;

                }catch (Exception ex){
                    ex.printStackTrace();
                }

                return null;
            }


            @Override
            protected void onPostExecute(JSONObject json) {

                if (progressDialog != null && progressDialog.isShowing()){
                    progressDialog.dismiss();
                }

                if (json != null){

                    try {

                        result = json.getString(TAG_MESSAGE);
//                        message =json.getString(TAG_STATUS);
                    }catch (Exception ex){
                        ex.printStackTrace();
                    }
                }


                if (message.equals("SUCCESS")){

                    Intent  intent = new Intent(getApplication(),OtherActivity.class);
                    startActivity(intent);
                    Toast.makeText(getApplicationContext(), "Login Successful.....", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        }

        LoginAsync la = new LoginAsync();
        la.execute(email,password);
    }

2 个答案:

答案 0 :(得分:1)

JSONObject j_res = new JSONObject(response);
String msg=j_res.getString("message");
if(msg.equalsIgnoreCase("Login successfully !")
{
  Intent  intent = new Intent(getApplication(),OtherActivity.class);
                    startActivity(intent);
}

试试这个

答案 1 :(得分:0)

UICollectionView