【问题标题】:How to parse json response, when the response does not contain header information当响应不包含标头信息时如何解析json响应
【发布时间】:2019-05-15 23:29:46
【问题描述】:

我正在尝试解析 json 响应,以便我可以从对象中获取元素,出现以下错误 A JSONObject 文本必须以 '{' at 1 [character 2 line 1] 开始

public static  String parseJsonResponse(String json){
    String uId ="";

    try {
         JSONObject jsonObj = new JSONObject(json);
         // String fname = jsonObj.getString("fname");
         //String lname = jsonObj.getString("lname");
         String aId = jsonObj.getString("id");
         uId = aId; 
     } catch (Exception e) {
         e.printStackTrace();
     }
    return uId;
}

这是使用邮递员的 json 响应,您会注意到没有标题

[
    {
        "id": "emplo000000000043567",
        "displayName": "Tester, user1",
           },
    {
        "id": "emplo000000000035386",
        "displayName": "Tester, User2",

    }
]

【问题讨论】:

  • 那不是 JSON 对象。这是一个 JSON 数组。

标签: java json parsing


【解决方案1】:

就像上面提到的评论一样,这是一个 JSON 数组,因此需要将其解析为 JSON 数组而不是 JSON 对象。只需使用您正在使用的库中提供的 JSONArray 等效项。

另一方面,对于上面的 JSON 响应,将其解析为 JSON 数组会失败,因为格式不正确。注意每个对象中每个最后一个键值末尾的逗号。这将导致解析器在尝试将其解析为 JSON 数组时失败。如果这是您在此处编写 sn-p 时的错误,请忽略本段。否则,如果那是实际的 JSON 响应,那么我想您需要在 Postman 论坛上提出一个新问题。

【讨论】:

    【解决方案2】:

    这个案例有几个想法。 这是我的。

    1. 带有一个json简单库[link].

    您可以简单地将您的库更改为具有 json 字符串解析器类的 json 简单库 然后在处理json对象之前使用instanceof方法进行检测。

    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    
    public static  String parseJsonResponse(String json){
        String uId ="";
    
        try {
            JSONParser parser = new JSONParser();
            Object whichone = parser.parse(json);
    
            if(whichone instanceof JSONObject)
            {
                JSONObject jsonObj = (JSONObject)whichone;
                 // String fname = jsonObj.getString("fname");
                 //String lname = jsonObj.getString("lname");
                if(jsonObj.containsKey("id"))
                    uId = (String)jsonObj.get("id");
           }
            else if(whichone instanceof JSONArray)
            {
                JSONArray jsonArr = (JSONArray)whichone;
                JSONObject jsonObj = null;
    
                for(int i = 0; i < jsonArr.size(); i++)
                {
                    jsonObj = (JSONObject) jsonArr.get(i);
                    if(jsonObj.containsKey("id"))
                    {
                        uId = (String)jsonObj.get("id");
                        System.out.println(uId);
                    }
    
                }
            }
            else if(whichone instanceof String)
            {
                System.out.println("1?????" + whichone.toString());
            }
            else
            {
                System.out.println("2?????" + whichone.toString());
            }
    
         } catch (Exception e) {
             e.printStackTrace();
         }
        return uId;
    }
    
    1. 从 json 异常中检测对象类型。 在异常处理过程中,您可以捕捉到某个字符串是 json 对象还是 json 数组。
    import org.json.JSONArray;
    import org.json.JSONObject;
    
    public static  String parseJsonResponse(String json){
        String uId ="";
    
        try {
            JSONObject jobj =  new JSONObject(json);
    
            if(jobj.has("id"))
                uId = jobj.getString("id");
    
            System.out.println(uId);
    
         } catch (org.json.JSONException e) {
             //e.printStackTrace();
    
             JSONArray jsonArr = new JSONArray(json);
             JSONObject jsonObj = null;
    
             for(int i = 0; i < jsonArr.length(); i++)
                {
                    jsonObj = jsonArr.getJSONObject(i);
                    if(jsonObj.has("id"))
                    {
                        uId = (String)jsonObj.get("id");
                        System.out.println(uId);
                    }
    
                }
         }
        return uId;
    }
    
    1. 用java工作。

    解析第一个字符后,无论是json对象还是数组,都可以找到。 (我认为它会起作用......)

    import org.json.JSONArray;
    import org.json.JSONObject;
    public static  String parseJsonResponse(String json){
        String uId ="";
    
        boolean isJobj = json.charAt(0) == '[';
    
        if(!isJobj) {
            JSONObject jobj =  new JSONObject(json);
    
            if(jobj.has("id"))
                uId = jobj.getString("id");
    
            System.out.println(uId);
    
         } else {
             JSONArray jsonArr = new JSONArray(json);
             JSONObject jsonObj = null;
    
             for(int i = 0; i < jsonArr.length(); i++)
                {
                    jsonObj = jsonArr.getJSONObject(i);
                    if(jsonObj.has("id"))
                    {
                        uId = (String)jsonObj.get("id");
                        System.out.println(uId);
                    }
    
                }
         }
        return uId;
    }
    

    祝你有美好的一天..

    【讨论】:

      【解决方案3】:

      首先,您的 json 格式错误。正确的 json 格式是:

      [
          {
              "id": "emplo000000000043567",
              "displayName": "Tester, user1"
          },
          {
              "id": "emplo000000000035386",
              "displayName": "Tester, User2"
          }
      ]
      

      现在,

      1. 您的响应是 JSON 数组。所以首先将解析后的对象分配到 JSON Array 中为 JSONArray array = (JSONArray) obj;
      2. 此 JSON 数组由两个 JSON 对象组成,因此请遍历数组,获取每个 JSON 对象并打印/返回键/值对,随心所欲。

      下面给出一个示例代码:(见逻辑)

      public static void parseJsonResponse(String json)
                  throws JsonParseException, JsonMappingException, IOException, ParseException {
              String aId ="";
              JSONParser parser = new JSONParser();
              Object obj = parser.parse(json);
              JSONArray array = (JSONArray) obj;
              for(int i=0;i<array.size();i++)
              {
                  JSONObject jsonObject = (JSONObject) array.get(i);
                  aId = (String) jsonObject.get("id");
                  System.out.println(aId);
              }
      
          }
      

      注意:我在这里使用了 json-simple java 库。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-15
        • 2011-06-15
        • 1970-01-01
        • 2012-04-03
        • 1970-01-01
        • 2017-05-21
        • 2019-08-29
        相关资源
        最近更新 更多