5

I am getting a crash on receiving a JSON string from our applications server. We believe that when an entry has "quotes", there are extra escapes added.

double escapes??

In android, how can I determine if I receive such a string , and how do I fix it from the Android side?

Here is our current response string processing:

public String processResponseString(String responseString) {
    if (responseString.startsWith("\"")) {
        responseString = responseString.substring(1, responseString.length());
    }
    if (responseString.endsWith("\"")) {
        responseString = responseString.substring(0, responseString.length() - 1);
    }
    responseString = EscapeUtil.unescapeString(responseString);
    return responseString;
}

Also, logcat does not include the entire json string after the crash, so I cannot see the actual string that is causing the crash.


Exception

 java.lang.ClassCastException: com.optiisolutions.housekeeping.model.OptiiAPI.OptiiError cannot be cast to java.util.Map
 at com.optiisolutions.housekeeping.network.OptiiHTTPClientRetroFit$2.success(OptiiHTTPClientRetroFit.java:186)
 at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)

OptiiHTTPClientRetroFit.java:186

optiiClient.postRequest(event.getRequest(), new Callback<Map<String, Object>>() {
    @Override
     public void success(Map<String, Object> stringObjectMap, Response response) {
         Log.d(TAG, "Successful response: " + stringObjectMap.toString());
          String result = (String) stringObjectMap.get(OPTII_RESULT_TYPE);
          String json = gson.toJson(stringObjectMap, Map.class);
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
WrightsCS
  • 50,551
  • 22
  • 134
  • 186

2 Answers2

3

I think you need to parse Map in to JSON Like below code by using JSONValue.

optiiClient.postRequest(event.getRequest(), new Callback<Map<String, Object>>() {
    @Override
     public void success(Map<String, Object> stringObjectMap, Response response) {
         Log.d(TAG, "Successful response: " + stringObjectMap.toString());
        // For JsonValue you need to add one jar file .
         String json= JSONValue.toJSONString(stringObjectMap);
         Log.d(TAG, "Successful json: " + json);
}

need to add jar javax.json-1.0.2.jar in gradle dependencies

dependencies {
    compile files('libs/javax.json-1.0.2.jar')
}

Download javax.json-1.0.2.jar Download from below link:

http://www.java2s.com/Code/Jar/j/Downloadjavaxjson102jar.htm

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
0

I tried to replicate your string. I'm assuming you want to keep the quotes. I could solve it with this simple algorithm, hope it works for you:

    public static String process(String s){
       String sep = "\\\\";
       String[] arr = s.split(sep);
       StringBuilder sb = new StringBuilder();

       for(String str : arr){
          sb.append(str);
       }

       return sb.toString();
    }  
Merve Sahin
  • 1,008
  • 2
  • 14
  • 26
  • 1
    this code will remove all backslashes from the escaped json string, which will make it impossible do unescape – nandsito Apr 09 '17 at 10:43
  • @nandsito I might have misunderstood what you want to achieve. I thought you were looking for a way to remove the escapes. However unescaping should be possible since a string is not mutable and therefore I return a new string. You could always save the original string – Merve Sahin Apr 09 '17 at 16:29
  • your method may work for the code sample the OP showed, but if the server returned a line feed `\n`, your method would transform it into an `n` char – nandsito Apr 09 '17 at 16:40
  • @nandsito Yes you are completely right. I changed the code, hope it works this time (ps this would only work if there are two slashes) – Merve Sahin Apr 09 '17 at 16:48
  • (after the edit) if the server sent an escaped reverse solidus, or backslash \\ your method would remove it from the response altogether. Actually i don't think this problem can be solved by filtering the input. I'm waiting for the OP to answer my questions so i can elaborate an answer – nandsito Apr 09 '17 at 16:53