无法转换为JSONObject

时间:2016-05-18 15:58:41

标签: android

您好我该如何解决此错误 当我在我的localhost尝试它工作正常但现在我收到此错误

org.json.JSONException: Value  ..... of type org.json.JSONArray cannot be converted to JSONObject

private void getdetails(String json){
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray result = jsonObject.getJSONArray(TAG_JSON_ARRAY);
            JSONObject c = result.getJSONObject(0);
            String mdp = c.getString("Password");
            String login = c.getString("Login");
            String type = c.getString("Type");
            String d = mdp;
            String d3 = type;
            if(d.equalsIgnoreCase(et_password.getText().toString())&& d3.equals("Delegue"))
            {
                Intent intent = new Intent(this,DetailsAccount.class);
                intent.putExtra("login", et_login.getText().toString());
                startActivity(intent);
            }
            else{
                Toast.makeText(MainActivity.this, "Merci de vérifier votre email et mot de passe", Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

1 个答案:

答案 0 :(得分:1)

尝试此功能

private void getdetails(String json){
    try {

        JSONArray result = new JSONArray(json);
        JSONObject c = result.getJSONObject(0);
        String mdp = c.getString("Password");
        String login = c.getString("Login");
        String type = c.getString("Type");
        String d = mdp;
        String d3 = type;
        if(d.equalsIgnoreCase(et_password.getText().toString())&& d3.equals("Delegue"))
        {
            Intent intent = new Intent(this,DetailsAccount.class);
            intent.putExtra("login", et_login.getText().toString());
            startActivity(intent);
        }
        else{
            Toast.makeText(MainActivity.this, "Merci de vérifier votre email et mot de passe", Toast.LENGTH_SHORT).show();
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

如果您的JSON字符串只是一个数组,则无法将其转换为对象。您只需从字符串本身构造一个JSON数组。