在条件语句中访问JSON对象

时间:2015-02-10 04:46:28

标签: android jsonobject

为了开发一个android应用程序,我需要检查json对象里面的值是否为空。我怎么能这样做?传递的参数是用户名,密码

private void invokeWS(RequestParams params) {
     prgDialog.show();
     AsyncHttpClient client = new AsyncHttpClient();
     client.get("url for restful web service", params, new AsyncHttpResponseHandler()
     {
         public void onSuccess(String response){
                             prgDialog.hide();

                             try {

                JSONObject obj = new JSONObject(response);
                    if(obj.length()==1){

                    Toast.makeText(getApplicationContext(), "You successfully logged in!", Toast.LENGTH_LONG).show();

                }
                else {
                    Toast.makeText(getApplicationContext(), " NOT a valid User", Toast.LENGTH_LONG).show();

                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                //Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
                 Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
                  e.printStackTrace();
            }
         }
         public void onFailure(int statusCode, Throwable error,String content){
             prgDialog.hide();
             if(statusCode == 404){
                 Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
             } 
             else if(statusCode == 500){
                 Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
             }
             else{
                 Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
             }
         }
     } );

}

2 个答案:

答案 0 :(得分:1)

then you should use this:

if(obj != null){

JSONArray data = obj.getJSONArray("validate")
if(data.length()==1){
        for (int i = 0; i < data.length(); i++) {

                    JSONObject user = data.getJSONObject(i);
                    String id = user.getString("id").toString()
                    String class = user.getString("class").toString()
                    String name = user.getString("name").toString()

                   Log.i("user Details ", "id = "+id+" class = "+class+" name = "+name);

                }
                    Toast.makeText(getApplicationContext(), "You successfully logged in!", Toast.LENGTH_LONG).show();

                }
                else {
                    Toast.makeText(getApplicationContext(), " NOT a valid User", Toast.LENGTH_LONG).show();

                }
    }else{
    Log.e("obj is null", "No date on JSON");
    }



      **try this in your try Block**

答案 1 :(得分:0)

  

我需要检查json对象中的值是否为空或   不。我怎么能这样做?

JSONObject有两种方法:

JSONObejct.has(String name)

如果在当前json对象中找到了请求的密钥,则返回true

JSONObejct.isNull(String name)

如果在json对象中找不到请求的密钥并且找到密钥但值为NULL,则返回true