当响应不包含头信息时如何解析json响应

我正在尝试解析 json 响应,以便我可以从对象中获取元素,出现以下错误 A JSONObject text must begin with '{' 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",


    }

]


MM们
浏览 112回答 3
3回答

慕的地6264312

就像上面提到的评论一样,这是一个 JSON 数组,因此需要将其解析为 JSON 数组而不是 JSON 对象。只需使用您正在使用的库中提供的 JSONArray 等效项。另一方面,对于上面的 JSON 响应,将其解析为 JSON 数组会失败,因为格式不正确。请注意每个对象中每个最后一个键值末尾的逗号。这会导致解析器在尝试将其解析为 JSON 数组时失败。如果这是您在此处编写代码片段时的错误,请忽略本段。否则,如果那是实际的 JSON 响应,那么我想您需要提出一个新问题……在 Postman 论坛上。

慕容708150

首先,你的json格式是错误的。正确的 json 格式应该是:[&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": "emplo000000000043567",&nbsp; &nbsp; &nbsp; &nbsp; "displayName": "Tester, user1"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": "emplo000000000035386",&nbsp; &nbsp; &nbsp; &nbsp; "displayName": "Tester, User2"&nbsp; &nbsp; }]现在,您的响应是 JSON 数组。因此,首先将解析后的对象分配到 JSON 数组中,如JSONArray array = (JSONArray) obj&nbsp;;这个 JSON 数组由两个 JSON 对象组成,因此遍历数组,获取每个 JSON 对象并打印/返回任何你想要的键/值对。示例代码如下:(见逻辑)public static void parseJsonResponse(String json)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws JsonParseException, JsonMappingException, IOException, ParseException {&nbsp; &nbsp; &nbsp; &nbsp; String aId ="";&nbsp; &nbsp; &nbsp; &nbsp; JSONParser parser = new JSONParser();&nbsp; &nbsp; &nbsp; &nbsp; Object obj = parser.parse(json);&nbsp; &nbsp; &nbsp; &nbsp; JSONArray array = (JSONArray) obj;&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0;i<array.size();i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject = (JSONObject) array.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aId = (String) jsonObject.get("id");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(aId);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }注意:我在这里使用了 json-simple java 库。

杨__羊羊

这个案例有几个想法。这是我的。使用 json 简单库[链接]。您可以简单地将您的库更改为 json 简单库,该库具有用于 json 字符串的解析器类,然后在处理 json 对象之前使用 instanceof 方法进行检测。import org.json.simple.JSONArray;import org.json.simple.JSONObject;import org.json.simple.parser.JSONParser;public static&nbsp; String parseJsonResponse(String json){&nbsp; &nbsp; String uId ="";&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; JSONParser parser = new JSONParser();&nbsp; &nbsp; &nbsp; &nbsp; Object whichone = parser.parse(json);&nbsp; &nbsp; &nbsp; &nbsp; if(whichone instanceof JSONObject)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObj = (JSONObject)whichone;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// String fname = jsonObj.getString("fname");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//String lname = jsonObj.getString("lname");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(jsonObj.containsKey("id"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uId = (String)jsonObj.get("id");&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; else if(whichone instanceof JSONArray)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray jsonArr = (JSONArray)whichone;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObj = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < jsonArr.size(); i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObj = (JSONObject) jsonArr.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(jsonObj.containsKey("id"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uId = (String)jsonObj.get("id");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(uId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if(whichone instanceof String)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("1?????" + whichone.toString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("2?????" + whichone.toString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;} catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e.printStackTrace();&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; return uId;}从 json 异常中检测对象类型。在异常处理过程中,无论某个字符串是 json 对象还是 json 数组,您都可以捕获它。import org.json.JSONArray;import org.json.JSONObject;public static&nbsp; String parseJsonResponse(String json){&nbsp; &nbsp; String uId ="";&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; JSONObject jobj =&nbsp; new JSONObject(json);&nbsp; &nbsp; &nbsp; &nbsp; if(jobj.has("id"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uId = jobj.getString("id");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(uId);&nbsp; &nbsp; &nbsp;} catch (org.json.JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONArray jsonArr = new JSONArray(json);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONObject jsonObj = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int i = 0; i < jsonArr.length(); i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObj = jsonArr.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(jsonObj.has("id"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uId = (String)jsonObj.get("id");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(uId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; return uId;}用java工作。解析第一个字符后,无论是json对象还是数组都可以找到。(我认为它会起作用......)import org.json.JSONArray;import org.json.JSONObject;public static&nbsp; String parseJsonResponse(String json){&nbsp; &nbsp; String uId ="";&nbsp; &nbsp; boolean isJobj = json.charAt(0) == '[';&nbsp; &nbsp; if(!isJobj) {&nbsp; &nbsp; &nbsp; &nbsp; JSONObject jobj =&nbsp; new JSONObject(json);&nbsp; &nbsp; &nbsp; &nbsp; if(jobj.has("id"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uId = jobj.getString("id");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(uId);&nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONArray jsonArr = new JSONArray(json);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONObject jsonObj = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int i = 0; i < jsonArr.length(); i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObj = jsonArr.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(jsonObj.has("id"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uId = (String)jsonObj.get("id");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(uId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; return uId;}祝你有美好的一天..
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java