有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用Java解析JSON如何处理不同的值?

我有一个服务器调用,返回我试图用Java解析的JSON。如果服务器上有错误,那么它将返回JSON并返回错误:error\u name

但如果没有错误,服务器将返回JSON中的数据

当我检查是否有错误时,当前设置解析的方式正在崩溃。以下是我所拥有的:

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

我尝试发送和解析此JSON的方式是否存在根本错误?感觉是这样的:)

请帮助我了解这样做的正确方法是什么 谢谢

以下是一个崩溃错误:

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\"}]

共 (2) 个答案

  1. # 1 楼答案

    您能在这两种情况下粘贴json吗

    通常,您应该始终将响应json封装在一个json对象中,这样当您查看响应时,您总是知道要查找什么。 比如说

    {“结果”:“确定”,数据:[{“数据1”:“值1”,“数据2”:“值2”}]

    {“结果”:“错误”}

    所以你的代码非常简单

    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*/ }
    

    -普雷马尔

  2. # 2 楼答案

    在没有错误的情况下,对ob.getString("error")的调用会引发您遇到的异常,因为对象中没有"error"键。在尝试获取密钥之前,可以使用JSONObject#has方法来测试密钥是否存在,或者遵循另一个答案中列出的类似于Premal的方法