Q:

JSON parsing with Java - how to handle different values?

I have a server call that returns JSON which I try to parse with Java.  If there is an error on the server, then it returns JSON with error:error_name
But if there is not error, the server returns data inside the JSON.  
The way I have the parsing set up currently is crashing when I check if there was an error.  Here is what I have:
try
{
    JSONArray obj = new JSONArray(result);
    if ( obj != null )
    {
        discussion.clear();
        if ( obj.length() == 0 )
        {
            DiscussionMessage message = new DiscussionMessage ( );                            
            discussion.add( message );
        }
        else
        {
            JSONObject ob = obj.getJSONObject(0);
            String error = ob.getString("error");
            if (error != null && 
                ( error.equals("no_problem_id") || 
                  error.equals("error_adding_suggested_solution_comment") || 
                  error.equals("no_recent_topic_id") ||
                  error.equals("no_comment") ||
                  error.equals("no_member_id") ||
                  error.equals("no_plan_id") ||
                  error.equals("error_duplicate_topic_comment") ) 
               )
            {
                {                
                    Toast.makeText(getApplicationContext(), "Could not get the current discussion.", Toast.LENGTH_LONG).show();                                 
                    sendEmail("Add business comment error response" , "Error response from server. Response: " + result);   
                }
            }
            else if ( error != null &&  error.equals("no_email_in_public_plan"))
            {
                Toast.makeText(getApplicationContext(),"Unexpected error. Please let us know about this" , Toast.LENGTH_LONG).show();    
                sendEmail("Error adding fundraising comment" , "Empty email adding fundraising plan comment");
            }
            else
            {
                try
                {
                    for ( int i = 0; i < obj.length(); i++ )
                    {
                        JSONObject o = obj.getJSONObject(i);
                        //String suggested_solution_id = o.getString("suggested_solution_id");
                        String comment = o.getString("comment");
                        String commenter_id = o.getString("commenter_id");
                        String comment_id = o.getString("comment_id");
                        String first_name = o.getString("first_name");
                        String is_private = o.getString("privacy");
                    }
                }
                catch ( Exception e )
                {
                }
            }
        }
    }
}
catch ( Exception e )
{
}

Is there something fundamentally incorrect about the way I am trying to send and parse this JSON? It feels so :)
Please help me understand what is the correct way of doing this
Thanks.
Here is a crash error:
Exception: No value for error , and result was: [{\"comment_id\":\"24\",\"plan_id\":\"20\",\"commenter_id\":\"1\",\"comment\":\"test\",\"solution_part\":\"1\",\"date\":\"2013-03-13\",\"first_name\":\"Alex\",\"privacy\":\"0\"},{\"comment_id\":\"25\",\"plan_id\":\"20\",\"commenter_id\":\"55018\",\"comment\":\"hi\",\"solution_part\":\"1\",\"date\":\"2013-03-13\",\"first_name\":\"Sddggh\",\"privacy\":\"0\"}]

A:

Can you please paste your json in both cases ? 
typically you should always enclose your response json in one json object so when you are looking at response you always know what to look for. 
so for e.g. 
{"result":"ok", data :[{"data1":"value1", "data2":"value2"}]} 
or 
{"result":"error"}
so your code is very simplified
JSONObject responseObj = new JSONObject(responseString);
String result = responseObject.get("result");
if(result.equalsIgnoreCase("ok") 
{ /* handle good case */ 
    JSONArray list = (JSONArray)responseObj.get("data");
    for(...) {}
}
else { /* handle error case*/ }

-Premal

