0

string から json オブジェクトを作成しようとしています。サーバーからこの文字列を取得し、「\」に置き換えます。それでもエラーが発生します。ここに私のjsonがあります

{
   "tasks":
   [
   {
      "id": "activiti$1942",
      "description": "review the doc",
      "dueDate": "9999-06-11 12:26:48 GMT+0530 (IST)",
      "status": "Not Yet Started",
      "priority": "2",
      "startDate": "2015-06-11 12:26:30 GMT+0530 (IST)",
      "type": "Review",
      "completeness": "0",
      "resources":
      [
         {
            "nodeRef": "workspace://SpacesStore/5d313010-5359-4749-8d8e-935bd073999c",
            "fileName": "plc fanuc links",
            "displayName": "plc fanuc links",
            "location":
            {
               "site": "hix-project",
               "container": "documentLibrary",
               "path": ""
            },
            "icon": "/images/filetypes/_default.gif"
         }
      ],
      "transitions":
      [
         {
            "id": "Next",
            "label": "Task Done"
         }
      ]
   }

   ]
}

これが私のJavaコードです

    BufferedReader breader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuilder responseString = new StringBuilder();
        String line = "";
        while ((line = breader.readLine()) != null) {
            responseString.append(line);
        }
        breader.close();
        String repsonseStr = responseString.toString();
        repsonseStr =  repsonseStr.replaceAll("\\s+","");
         repsonseStr = repsonseStr.replace("\"", "\\\"");

        System.out.println("repsonseStr =" + repsonseStr);




        JSONObject object= new JSONObject(repsonseStr);

    //JSONArray tsmresponse = (JSONArray) myResponse.get("tasks");

    ArrayList<String> list = new ArrayList<String>();


    org.json.JSONArray array = object.getJSONArray("tasks");
    for(int i=0; i<array.length(); i++){
        try {
            list.add(array.getJSONObject(i).getString("id"));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    System.out.println(list);

私が渡しているrepsonseStr値は

{\"tasks\":[{\"id\":\"activiti$1942\",\"description\":\"reviewthedoc\",\"dueDate\":\"9999-06-1216:01:47GMT+0530(IST)\",\"status\":\"NotYetStarted\",\"priority\":\"2\",\"startDate\":\"2015-06-1112:26:30GMT+0530(IST)\",\"type\":\"Review\",\"completeness\":\"0\",\"resources\":[{\"nodeRef\":\"workspace://SpacesStore/5d313010-5359-4749-8d8e-935bd073999c\",\"fileName\":\"plcfanuclinks\",\"displayName\":\"plcfanuclinks\",\"location\":{\"site\":\"hix-project\",\"container\":\"documentLibrary\",\"path\":\"\"},\"icon\":\"/images/filetypes/_default.gif\"}],\"transitions\":[{\"id\":\"Next\",\"label\":\"TaskDone\"}]}]}

誰でも助けることができます。エラーは

org.json.JSONException: 1 に値がありません [文字 2 行 1]

4

1 に答える 1

0

置換を削除すると、サーバー出力は有効な JSON になります。

JSONパーサーとしてJackson(https://github.com/FasterXML/jacksonを参照)を使用すると、次のコードが機能します(ここでは、質問で指定された入力をファイルから読み取るファイル入力ストリームを使用しますが、test.json入力に置き換えますストリームresponse.getEntity().getContent()は同じように機能します):

  try {
    FileInputStream stream = new FileInputStream("test.json");
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = mapper.readTree(stream);
    JsonNode tasks = node.get("tasks");
    for (JsonNode task : tasks) {
      System.out.println(task.toString());
    }
  } catch (Throwable throwable) {
    throwable.printStackTrace();
  }

ジャクソンのmaven依存関係:

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.5.4</version>
</dependency>
于 2015-06-12T11:01:52.907 に答える