简体   繁体   中英

java.lang.String cannot be converted to JSONObject in java

I am calling an API and getting this in response

"{\r\n  \"wrongUserLogInIdOrPassword\": true,\r\n  \"isEmailVerificationRequired\": false,\r\n  \"displayEmail\": null,\r\n  \"isPhoneVerificationRequired\": false,\r\n  \"displayPhone\": null,\r\n  \"isUserLockedOut\": false,\r\n  \"lockedOutEndDateTime\": 0,\r\n  \"isProgressToken\": false,\r\n  \"progressToken\": null,\r\n  \"isSecondPasswordRequired\": false,\r\n  \"secondPasswordToken\": null,\r\n  \"isTwoFactorRequired\": false,\r\n  \"twoFactorToken\": null,\r\n  \"logInId\": {\r\n    \"value\": \"xxxx@gmail.com\",\r\n    \"error\": \"Wrong Login id or password\"\r\n  },\r\n  \"password\": {\r\n    \"value\": \"\",\r\n    \"error\": \"Wrong Login id or password\"\r\n  },\r\n  \"invalidData\": false,\r\n  \"somethingWentWrong\": false,\r\n  \"error\": null\r\n}"

then I tried to convert response in JSON object by using this

 v = v.replaceAll("(\r\n|\n)", "");
            return new JSONObject(v).toString();

I also tried to replace "\n\r" but it's not working.

can anyone help?

在此处输入图像描述

@sourabhdevpura, I've run this code, and it works fine. Probably the problem is that the method's result ( String from return new JSONObject(v).toString(); ) is assigned to a variable of type JSONObject .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM