【问题标题】:JSON Object ExceptionJSON 对象异常
【发布时间】:2019-01-18 03:55:09
【问题描述】:

我正在编写一个 Web 服务-REST。 我收到的是 text_plain 格式。

我的数据示例:

{"data_log":
    {
       "methodClass": [{"methodName": 1, "methodStatus": "1"}, 
           {"methodName": 2, "methodStatus": "1"}]
    }
}

在我的 Restful Webservice 中,我尝试读取数据,但出现错误 500 Internal Server Error-JSON Object not found。

public int adaptiveAuth(String objDataLog){ 
        logWriter("objDataLog:"+objDataLog);
        JSONObject obj = new JSONObject(objDataLog);

 JSONArray methodClassObj=(JSONArray)obj.get("methodClass"); 

        if (methodClassObj != null) {
             JSONObject methodObj;
             String entrymethodName, entrymethodStatus;
             for (Object o : methodClassObj) {
                methodObj = (JSONObject) o;
                entrymethodName = String.valueOf(methodObj.get("methodName"));
                entrymethodStatus = String.valueOf(methodObj.get("methodStatus"));

                logWriter("entrymethodName:"+entrymethodName);
                logWriter("entrymethodStatus:"+entrymethodStatus);
             }

        }
}

我已经尝试使用下面的代码,但它仍然给我错误。

 String methodClass= obj.getJSONObject("data_log").getString("methodClass"); 

我希望将当前数据放入一个二维数组中,如下所示:

[1,1],[2,1]

谁能给我一些关于如何解决这个问题的建议?

【问题讨论】:

    标签: json rest web-services


    【解决方案1】:
     **Try this code for parsing json data**      
    
                   ArrayList<HashMap<String, String>> list;
                  JSONObject jsonObj = new JSONObject(jsonStr);
    
                   // Getting JSON Array node
                   JSONArray b= jsonObj.getJSONArray("methodClass");
    
                   // looping through All data
                   for (int i = 0; i < b.length(); i++) {
                      JSONObject c = b.getJSONObject(i);
    
                      String entrymethodName = c.getJSONObject("methodClass").getString("methodName");
                      String entrymethodStatus = c.getJSONObject("methodClass").getString("methodStatus");
    
                      // tmp hash map for storing values
                      HashMap<String, String> co = new HashMap<>();
    
                      // adding each child node to HashMap key => value
                      co.put(entrymethodName, entrymethodStatus );
    
    
                      // adding co to  list
                      list.add(co);
    

    也检查一下 - https://gist.github.com/codebutler/2339666

    【讨论】:

    • 嗨,ArrayList> 列表; JSONObject jsonObj = new JSONObject(objDataLog); JSONArray b= jsonObj.getJSONArray("methodClass");它给了我错误:org.json.JSONException: JSONObject["methodClass"] not found.
    • JSONArray methodClass= (JSONArray) obj.get("methodClass");也返回相同的错误
    • 你好,代码中的联系人是一个数组吗?我没有看到任何声明
    • 我的错误,现在再试一次。在代码之前编辑,这也是
    • 我仍然遇到同样的错误。未找到 JSON 对象方法类。 @POST @Path("/post") @Consumes(MediaType.TEXT_PLAIN) @Produces(MediaType.TEXT_PLAIN) 我正在发送纯文本。
    【解决方案2】:

    我用String改成这样的格式:

    { "方法集": [ { “方法名”:1, “方法状态”:“1” }, { “方法名”:2, “方法状态”:“1” } ] }

    对于代码:

      ArrayList<String> listMethod = new ArrayList<String>();    
        JSONArray arr=(JSONArray)obj.get("methodClass"); 
    
        if (arr != null) {
            JSONObject objMethod;
            String MethodName, MethodStatus;
            for (Object o : arr) {
                objMethod = (JSONObject) o;
                MethodName = String.valueOf(objMethod.get("methodName"));
                MethodStatus = String.valueOf(objMethod.get("methodStatus"));
    
                int resultMethodName = Integer.parseInt(MethodName);    
                int resultMethodStatus = Integer.parseInt(MethodStatus);    
    
                logWriter("MethodStatus"+resultMethodName);
                logWriter("MethodName"+resultMethodStatus);
    
                listMethod.add("(" + MethodName + "," + MethodStatus + ")");
    
                logWriter("listMethod is :     "+listMethod);  
    
    
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-06
      • 2017-03-24
      • 2018-01-04
      • 1970-01-01
      • 2017-04-22
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多