簡體   English   中英

如何將字符串轉換為 JSONObject?

[英]How to convert String to JSONObject?

我通過將數據字符串更改為 jsonobject 遇到了障礙,是他的 org.json.JSONException 腳本錯誤:值

這個編碼我正在嘗試

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

將字符串轉換為 JSONObject 有兩個條件:

1 . 字符串應該是正確的 Json 格式。

2 . 將代碼放在 try catch 塊中。

例子:

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":""}}"

現在將此字符串轉換為 JSONObject

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

}

嘗試這個,

如果您調用您的服務並嘗試將該代碼粘貼到您的 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