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

标签 java json

我有一个返回 JSON 的服务器调用,我尝试使用 Java 对其进行解析。如果服务器上出现错误,则返回带有 error:error_name 的 JSON

但如果没有错误,服务器会返回 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\"}]

最佳答案

您能在这两种情况下粘贴您的 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*/ }

-普雷马尔

关于java - 使用 Java 解析 JSON - 如何处理不同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15377955/

相关文章:

javascript - 如何访问 JSON 文件的一部分

ios - Swift 4.2 - 解码相同 key 不同类型的 JSON

java - 需要最简单的方法来对齐 JButton

java - 注销旧 session 并在 Java Web 应用程序中使用相同的用户 ID 和密码继续新 session

java - 使用 gson 解析 JSON 数组

ios - 如何在 iOS 中解析 mapquest geocode JSON

java - 如何使用 jSTL 在原始值旁边打印随机值?

java - 数据库驱动类动态加载

javascript - 我们如何为另一个页面发送隐藏在 php 中的 jquery 金额字段以检索金额值?

c# - 从url读取Json,无需下载