2

I get a JSON response as

{
"edges": [],
"nodes": []
}

how to check if the objects has null values and handle the case??

JSONObject jobj = new JSONObject(line);
JSONArray jArray = jobj.getJSONArray("edges");
if(jArray.length()!=0)
{    
  for(int i=0;i<jArray.length();i++){
  JSONObject json_data = jArray.getJSONObject(i);
  x.add((float) json_data.getInt("x"));
  y.add((float) json_data.getInt("y"));
end

This retrurns me : org.json.JSONException: end of input at character 0 of

1
  • 1
    Does that exception throw at this line, by any chance? JSONObject jobj = new JSONObject(line); I suspect you aren't parsing the response you think you are, since the exception message indicates you tried to parse an empty string. The parsing happens on your first line; the rest is fluff. Commented Dec 5, 2012 at 20:42

4 Answers 4

3

try this one:

String jsonString = "{ "edges": [], "nodes": [] }";

JSONObject jsonObject = new JSONObject(jsonString);

if( jsonObject.isNull("edges") == false) {
//do sth
}

if( jsonObject.isNull("nodes") == false) {
//do sth
}

you can also check if you have some particular key in your json by jsonObject.has("edges")

you are passing some \line\ variable to the JSONObject constructor. make sure that this variable contains your whole json string like this one in my example and not something like "{" or ' "edges": [] ' maybe the problem is in your json source like dokkaebi suggested in comment

Sign up to request clarification or add additional context in comments.

2 Comments

are u sure this will work because isNull just check key is exist in JsonObject or not
isNull check if key exist or if it have null value has() method checks only some key existence this is what documentation says "Determine if the value associated with the key is null or if there is no value."
2

you can check as:

JSONObject jobj = new JSONObject(line);
if (jobj.getJSONArray("edges").length() == 0) {

    System.out.println("JSONArray is null");      
 }
 else{
      System.out.println("JSONArray is not null");
      //parse your string here         
     }

6 Comments

what error it's showing? because you have two json Array inside JSONObject so JSONObject is not null. now you only need to check JSONArray is empty or not
org.json.JSONException: end of input at character 0 of
@user1494754 : if still facing same issue then check jArray.length() > -1 because if it's empty then it contain less then zero element . as you known also indexing start from 0
Nope.No luck.It did the same
@user1494754 : you have tried jArray.length() > -1 condition ?
|
2

try this on. I am showing example only for only one array depending on flag value you can show proper error message or on success you can bind parsed data to UI component.

String impuStr = "{\"edges\": [],\"nodes\": []}";

String flag = serverResponse(impuStr);

private String serverResponse(String jsonStr) { String flag = "success";

    JSONObject jobj;
    try {
        jobj = new JSONObject(jsonStr);

        JSONArray jArrayEdges = jobj.getJSONArray("edges");
        if(jArrayEdges != null && jArrayEdges.length() > 0)
        {    
          for(int i=0;i<jArrayEdges.length();i++)
          {
              JSONObject json_data = jArrayEdges.getJSONObject(i);
              // process data here
          }
         }else
             flag = "edges_list_empty";

    } catch (JSONException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
        flag = "failure";
    }

    return flag;
}

Comments

0

Using simple java rules. Check if the array is empty, if the array does not exist and you try to get it, it just returns null. Just handle it. Don't continue parsing if you know its going to fail. Just exist gracefully.

if (myObj != null)
{
  ... process
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.