Я пытаюсь создать объект json из строки. Я получаю эту строку с сервера, а затем заменяю «с помощью». но все равно я получаю ошибку. вот мойJson Exception при создании объекта 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]
* Почему * вы заменить ' "'с' \"'? Исходная строка действительна JSON - ваша версия с munged - нет. –
которые вы используете? org.json? –
Вы можете использовать [JSON validator] (http://jsonlint.com/), чтобы проверить ваш json после любых замен/преобразований. – plastique