我从网址获得okhttp库android java的响应,并将该响应转换为string。下面是响应字符串:
这是响应字符串:"{\"data\":{\"refrenceCode\":\"upfF+kMKv4Q=\",\"identityTypeId\":\"NV25GlPuOnQ=\",\"idNumber\":\"1000\",\"dateOfBirth\":19961004,\"registrationDate\":\"2020-05-12T12:03:47.647\",\"mobile\":\"0022343 \",\"regionId\":\"NV25GlPuOnQ=\",\"cityId\":\"NV25GlPuOnQ=\",\"carTypeId\":\"NV25GlPuOnQ=\",\"carNumber\":\"aa 000\"},\"status\":true,\"errorCodes\":[]}"
当我尝试使用以下方法将该字符串转换为json对象时;JSONObject jsonobj = new JSONObject(responsestring);
我得到以下json异常:org.json.JSONException: Value {"data":{"refrenceCode":"upfF+kMKv4Q=","identityTypeId":"NV25GlPuOnQ=","idNumber":"1000","dateOfBirth":19961004,"registrationDate":"2020-05-12T12:03:47.647","mobile":"0022343 ","regionId":"NV25GlPuOnQ=","cityId":"NV25GlPuOnQ=","carTypeId":"NV25GlPuOnQ=","carNumber":"aa 000"},"status":true,"errorCodes":[]} of type java.lang.String cannot be converted to JSONObject
如何解决?
最佳答案
这是基于注释中的集体工作的解决方案:
responsestring = responsestring.substring(1, responsestring.length - 1).replace("\\\"", "\"");
感谢@ chrylis-onstrike-的观察,它对您有所帮助。
关于java - 如何将响应字符串转换为json对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61787782/