简体   繁体   English

如何将字符串转换为 JSONObject?

[英]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我通过将数据字符串更改为 jsonobject 遇到了障碍,是他的 org.json.JSONException 脚本错误:值

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 :将字符串转换为 JSONObject 有两个条件:

1 . 1 . The string should be in proper Json format .字符串应该是正确的 Json 格式。

2 . 2 . Put code in try catch block .将代码放在 try catch 块中。

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现在将此字符串转换为 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如果您调用您的服务并尝试将该代码粘贴到您的 isSucsess 部分中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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