简体   繁体   中英

How to convert String to JSONObject?

I have an obstacle by changing the data string into a jsonobject, is his org.json.JSONException script error: Value

This coding I am trying

btnLogin.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            String username = inputUser.getText().toString().trim();
            String password = inputPassword.getText().toString().trim();

            // Check for empty data in the form
            if (username.trim().length() > 0 && password.trim().length() > 0) {
                Map<String,String> params = new HashMap<>();
                params.put("username", inputUser.getText().toString());
                params.put("password", inputPassword.getText().toString());
                sendPostRequest(params);
            } else {
                // Prompt user to enter credentials
                Toast.makeText(getApplicationContext(),
                        "Silahkan Masukan Username dan Password!", Toast.LENGTH_LONG)
                        .show();
            }
        }
    });

public void sendPostRequest(Map<String, String> params) {
    showDialog();
    RequestQueue queue = Volley.newRequestQueue(this);
    String url = "http://www.chris-chris.webege.com/login.php";

    mCustomRequest = new CustomRequest(Request.Method.POST,
            url, params, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Intent intent = new Intent(Login.this,
                    Coba.class);
            startActivity(intent);
            finish();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("Error", error.getMessage());
            hidepDialog();
            Toast.makeText(Login.this, "Belum Terhubung Internet! ", Toast.LENGTH_SHORT).show();
        }
    });
    mCustomRequest.setRetryPolicy(new DefaultRetryPolicy(Template.VolleyRetryPolicy.SOCKET_TIMEOUT,
            Template.VolleyRetryPolicy.RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    queue.add(mCustomRequest);
}

There are two conditions to convert a string to JSONObject :

1 . The string should be in proper Json format .

2 . Put code in try catch block .

Example:

String jsonString  = "{"status":"1","message":"Login successfully","data":{"uid":"1","email":"baleshwar@gmail.com","password":"202cb962ac59075b964b07152d234b70","name":"","role":"1","gender":"","address":"","image":"","is_active":"0","last_login":"0000-00-00 00:00:00","device_id":"0","created_at":"2015-06-03 06:01:02","updated_at":"2015-06-03 11:01:02","location":"","dob":"0000-00-00","descriptions":""}}"

Now convert this string to JSONObject

    try{
        JSONObject jsonResponse = new JSONObject(result);
    }catch(Exception e){
    e.printStackTrace();

}

Try this,

If you call your service and try to paste that code inside your isSucsess part

JSONObject jObject = jsonObject.getJSONObject("jsonobjectname");    
tvTitle.setText(jObject.getString("your string name"));

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