Json Exception while creating json object from a string

3.7k views Asked by At

I'm trying to create a json object from a string . i am getting this string from a server and then replace " with \". but still i'm getting error. here is my 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"
         }
      ]
   }

   ]
}

Here is my java code

    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 value which i'm passing is

{\"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\"}]}]}

can anybody help. The error is

org.json.JSONException: Missing value at 1 [character 2 line 1]

1

There are 1 answers

0
Remigius Stalder On

remove the replacements, the server output is valid JSON.

using Jackson (see https://github.com/FasterXML/jackson) as JSON parser, the following code works (here I use a file input stream that reads the input given in the question from the file test.json, but replacing it with the input stream response.getEntity().getContent() will work identically):

  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 for Jackson:

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.5.4</version>
</dependency>