Remove all occurrences of \ from string

后端 未结 6 1264
挽巷
挽巷 2020-12-03 03:12

I am trying to get an array of objects from the server, using JSON.

The server sends me the following string.

\"[{\\\"DealComment\\\":null,\\\"DealVo         


        
6条回答
  •  清歌不尽
    2020-12-03 03:35

    It looks like your incoming string is doubly JSON encoded. You should decode it, then decode that again.

    Here's my best guess as to how you might do that in Java:

    JSONArray resultArray = new JSONArray(new JSONString(jsonFormattedString));
    

    I'm assuming here that JSONString is a type. Your actual solution may vary.

    Under normal circumstances, I'd expect a service to give you JSON straight up. It appears that this services is giving you a string (encoded according to the JSON spec) which contains JSON.

    It's the difference between the following:

    String someJSON = "[0, 1, 2]";
    String doublyEncodedJSON = "\"[0, 1, 2]\"";
    

    Notice the extra leading and trailing quotes? That's because the latter is a string of JSON. You'd have to decode it twice to get the actual object.

提交回复
热议问题