Discussion:
[dart-misc] Remove escape sequences from string
Abhishek Aggarwal
2018-10-12 07:27:49 UTC
Permalink
To decode API response string to JSON, *json.decode()* works fine. This
will parse a JSON string similar to
{ “Response” : {“Responsecode” : “1” , “Response” : “Success”}}


But in my case, the response is in the form :
“{\”Response\” : {\”Responsecode\” : \”0\” , \”Response\” : \”Success\”}}”
*json.decode() *won’t work. I searched but couldn’t find how to unescape
characters in a string. Please help
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/45641080-337b-4a3b-8c8b-20512363fd3c%40dartlang.org.
Valentin Olchowski
2018-10-12 16:15:12 UTC
Permalink
There is a string methode "replaceAll(from, to)" where from is replaced by
to. So you can try this on the json string like replaceAll("/", "").
Post by Abhishek Aggarwal
To decode API response string to JSON, *json.decode()* works fine. This
will parse a JSON string similar to
{ “Response” : {“Responsecode” : “1” , “Response” : “Success”}}
“{\”Response\” : {\”Responsecode\” : \”0\” , \”Response\” : \”Success\”}}”
*json.decode() *won’t work. I searched but couldn’t find how to unescape
characters in a string. Please help
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/45641080-337b-4a3b-8c8b-20512363fd3c%40dartlang.org
<https://groups.google.com/a/dartlang.org/d/msgid/misc/45641080-337b-4a3b-8c8b-20512363fd3c%40dartlang.org?utm_medium=email&utm_source=footer>
.
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAGpBjM%2B3CfbS-fMogNys5ppbPfZdAXja2AW79o6mHYEeOOoeGA%40mail.gmail.com.
Abhishek Aggarwal
2018-10-12 16:33:35 UTC
Permalink
You mean replace "\" with "".
It seems to work in this case, but if slash is itself in the json-data, will also be removed.
Also, if double quotes are present, then it will also remove its escape sequence.

For example,
{\"key\": \"abc\"de\"}, removing slashes will result in {"key": "abc"de"}, which will create problem while json parsing.

But the result should be {"key": "abc\"de"}, which clearly shows that the value of key is abc"de
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/81c4bfb3-2cc7-42b6-a8cf-33d7dc464ed7%40dartlang.org.
'Samuel Rawlins' via Dart Misc
2018-10-12 16:34:09 UTC
Permalink
Yeah the String you have been given as a response is not properly encoded
JSON. A JSON key must be a string. A string is defined as a `"` character,
then characters, then a `"` character, _not_ the two-character sequence of
`\` and `"`, followed by characters.

Replacing with String.replaceAll might sort of work... but it's error
prone. If there is an escaped backslash inside a string, it would be
deleted... yuck. Better maybe to fix the source of the problem, where the
response is being encoded.

On Fri, Oct 12, 2018 at 9:15 AM Valentin Olchowski <
Post by Valentin Olchowski
There is a string methode "replaceAll(from, to)" where from is replaced by
to. So you can try this on the json string like replaceAll("/", "").
Post by Abhishek Aggarwal
To decode API response string to JSON, *json.decode()* works fine. This
will parse a JSON string similar to
{ “Response” : {“Responsecode” : “1” , “Response” : “Success”}}
“{\”Response\” : {\”Responsecode\” : \”0\” , \”Response\” : \”Success\”}}”
*json.decode() *won’t work. I searched but couldn’t find how to unescape
characters in a string. Please help
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/45641080-337b-4a3b-8c8b-20512363fd3c%40dartlang.org
<https://groups.google.com/a/dartlang.org/d/msgid/misc/45641080-337b-4a3b-8c8b-20512363fd3c%40dartlang.org?utm_medium=email&utm_source=footer>
.
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/CAGpBjM%2B3CfbS-fMogNys5ppbPfZdAXja2AW79o6mHYEeOOoeGA%40mail.gmail.com
<https://groups.google.com/a/dartlang.org/d/msgid/misc/CAGpBjM%2B3CfbS-fMogNys5ppbPfZdAXja2AW79o6mHYEeOOoeGA%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CACshfx1-oUpLq51h7yof5rWNEU7WMezjsDjkKba6STTqrp%2BH1Q%40mail.gmail.com.
Abhishek Aggarwal
2018-10-12 16:43:13 UTC
Permalink
I can't make change in the API. Also, I have been using these APIs for almost a year in my android projects, there's a java function StringEscapeUtils.unescapeJson(), which works perfectly for this response, but I couldn't find any such function in dart.
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/f4184281-b7dd-4d0c-8d99-956a91dfcd04%40dartlang.org.
Abhishek Aggarwal
2018-10-13 05:53:35 UTC
Permalink
Sorry, the example I gave earlier wasn't right

Suppose the value of key is *abc"de*. So, json string would be *{"key":
"abc\"de"}*
And the response would /be like *{\"key\": \"abc\\\"de\"}*.
Hence, after unescaping the sequences, the desired json would be *{"key":
"abc\"de"}*

On Fri, Oct 12, 2018 at 10:04 PM 'Samuel Rawlins' via Dart Misc <
Post by 'Samuel Rawlins' via Dart Misc
Yeah the String you have been given as a response is not properly encoded
JSON. A JSON key must be a string. A string is defined as a `"` character,
then characters, then a `"` character, _not_ the two-character sequence of
`\` and `"`, followed by characters.
Replacing with String.replaceAll might sort of work... but it's error
prone. If there is an escaped backslash inside a string, it would be
deleted... yuck. Better maybe to fix the source of the problem, where the
response is being encoded.
On Fri, Oct 12, 2018 at 9:15 AM Valentin Olchowski <
Post by Valentin Olchowski
There is a string methode "replaceAll(from, to)" where from is replaced
by to. So you can try this on the json string like replaceAll("/", "").
Post by Abhishek Aggarwal
To decode API response string to JSON, *json.decode()* works fine. This
will parse a JSON string similar to
{ “Response” : {“Responsecode” : “1” , “Response” : “Success”}}
\”Success\”}}”
*json.decode() *won’t work. I searched but couldn’t find how to
unescape characters in a string. Please help
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google
Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/45641080-337b-4a3b-8c8b-20512363fd3c%40dartlang.org
<https://groups.google.com/a/dartlang.org/d/msgid/misc/45641080-337b-4a3b-8c8b-20512363fd3c%40dartlang.org?utm_medium=email&utm_source=footer>
.
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/CAGpBjM%2B3CfbS-fMogNys5ppbPfZdAXja2AW79o6mHYEeOOoeGA%40mail.gmail.com
<https://groups.google.com/a/dartlang.org/d/msgid/misc/CAGpBjM%2B3CfbS-fMogNys5ppbPfZdAXja2AW79o6mHYEeOOoeGA%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/CACshfx1-oUpLq51h7yof5rWNEU7WMezjsDjkKba6STTqrp%2BH1Q%40mail.gmail.com
<https://groups.google.com/a/dartlang.org/d/msgid/misc/CACshfx1-oUpLq51h7yof5rWNEU7WMezjsDjkKba6STTqrp%2BH1Q%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAAqCwRV2_PLsshgDECJKvBR1nN8B%3DD4cTtJhjvvyGiUQHs473g%40mail.gmail.com.
Danny Tuppeny
2018-10-13 09:08:25 UTC
Permalink
*(I think my other email account got stuck in a mod queue, so a dupe might
come through too...)*

If you call json.decode() twice, I think you'll get what you need. The
value you have is what you'd get if you encoded twice (eg. you encode
something to a JSON string, then encoded that string). You may be able to
fix it "upstream" by avoiding the second encoding, but decoding twice may
be the simplest fix (esp. if there are already other consumers of the API).
Post by Abhishek Aggarwal
To decode API response string to JSON, *json.decode()* works fine. This
will parse a JSON string similar to
{ “Response” : {“Responsecode” : “1” , “Response” : “Success”}}
“{\”Response\” : {\”Responsecode\” : \”0\” , \”Response\” : \”Success\”}}”
*json.decode() *won’t work. I searched but couldn’t find how to unescape
characters in a string. Please help
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/af995dec-7a36-416a-b3f1-6ae43f70a207%40dartlang.org.
'Danny Tuppeny' via Dart Misc
2018-10-13 07:30:19 UTC
Permalink
json.decode can decode strings too, so you should be able to just call it
twice. The first time it'll return you a string (where the escape
characters have been decoded) and the second time it'll decode that string
into the map:

import 'dart:convert';

void main() {
var a = r'''"{\"Response\" : {\"Responsecode\" : \"0\" , \"Response\" :
\"Success\"}}"''';
var b = json.decode(json.decode(a));
print(b['Response']['Responsecode']); // 0
print(b['Response']['Response']); // Success
}
Post by Abhishek Aggarwal
To decode API response string to JSON, *json.decode()* works fine. This
will parse a JSON string similar to
{ “Response” : {“Responsecode” : “1” , “Response” : “Success”}}
“{\”Response\” : {\”Responsecode\” : \”0\” , \”Response\” : \”Success\”}}”
*json.decode() *won’t work. I searched but couldn’t find how to unescape
characters in a string. Please help
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/4d7bacd1-3364-4c14-8b1c-ce05170dcdb6%40dartlang.org.
Abhishek Aggarwal
2018-10-14 15:32:20 UTC
Permalink
I worked. Thanks a lot.

How can I convert this json array to list of a class (say Class Response
having string variables as Responsecode and Response)?
Does dart has something similar to java's gson?

On Sat 13 Oct, 2018, 11:04 PM 'Danny Tuppeny' via Dart Misc, <
Post by 'Danny Tuppeny' via Dart Misc
json.decode can decode strings too, so you should be able to just call it
twice. The first time it'll return you a string (where the escape
characters have been decoded) and the second time it'll decode that string
import 'dart:convert';
void main() {
\"Success\"}}"''';
var b = json.decode(json.decode(a));
print(b['Response']['Responsecode']); // 0
print(b['Response']['Response']); // Success
}
Post by Abhishek Aggarwal
To decode API response string to JSON, *json.decode()* works fine. This
will parse a JSON string similar to
{ “Response” : {“Responsecode” : “1” , “Response” : “Success”}}
“{\”Response\” : {\”Responsecode\” : \”0\” , \”Response\” : \”Success\”}}”
*json.decode() *won’t work. I searched but couldn’t find how to unescape
characters in a string. Please help
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/4d7bacd1-3364-4c14-8b1c-ce05170dcdb6%40dartlang.org
<https://groups.google.com/a/dartlang.org/d/msgid/misc/4d7bacd1-3364-4c14-8b1c-ce05170dcdb6%40dartlang.org?utm_medium=email&utm_source=footer>
.
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAAqCwRWD%3Dh02prNCO4M82NW7XqvLNeLRXC10vTekh_L8x4XkGA%40mail.gmail.com.
Abhishek Aggarwal
2018-10-15 06:33:22 UTC
Permalink
Please post your answer in the following stackoverflow post, I will mark it
as the right answer.
https://stackoverflow.com/questions/52774561/flutter-remove-escape-sequence-in-dart
.
Post by Abhishek Aggarwal
I worked. Thanks a lot.
How can I convert this json array to list of a class (say Class Response
having string variables as Responsecode and Response)?
Does dart has something similar to java's gson?
On Sat 13 Oct, 2018, 11:04 PM 'Danny Tuppeny' via Dart Misc, <
Post by 'Danny Tuppeny' via Dart Misc
json.decode can decode strings too, so you should be able to just call it
twice. The first time it'll return you a string (where the escape
characters have been decoded) and the second time it'll decode that string
import 'dart:convert';
void main() {
\"Success\"}}"''';
var b = json.decode(json.decode(a));
print(b['Response']['Responsecode']); // 0
print(b['Response']['Response']); // Success
}
Post by Abhishek Aggarwal
To decode API response string to JSON, *json.decode()* works fine. This
will parse a JSON string similar to
{ “Response” : {“Responsecode” : “1” , “Response” : “Success”}}
\”Success\”}}”
*json.decode() *won’t work. I searched but couldn’t find how to
unescape characters in a string. Please help
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/4d7bacd1-3364-4c14-8b1c-ce05170dcdb6%40dartlang.org
<https://groups.google.com/a/dartlang.org/d/msgid/misc/4d7bacd1-3364-4c14-8b1c-ce05170dcdb6%40dartlang.org?utm_medium=email&utm_source=footer>
.
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAAqCwRXDh4%2B%3DsUU%3DCE9tCnmK2vi6mXXT_u0P12n5Lzi7Hs%3Da%2BQ%40mail.gmail.com.
ravi teja
2018-10-15 07:54:15 UTC
Permalink
Try

https://pub.dartlang.org/packages/jaguar_serializer
or
https://pub.dartlang.org/packages/json_serializable
Post by Abhishek Aggarwal
Please post your answer in the following stackoverflow post, I will mark
it as the right answer.
https://stackoverflow.com/questions/52774561/flutter-remove-escape-sequence-in-dart
.
Post by Abhishek Aggarwal
I worked. Thanks a lot.
How can I convert this json array to list of a class (say Class Response
having string variables as Responsecode and Response)?
Does dart has something similar to java's gson?
On Sat 13 Oct, 2018, 11:04 PM 'Danny Tuppeny' via Dart Misc, <
Post by 'Danny Tuppeny' via Dart Misc
json.decode can decode strings too, so you should be able to just call
it twice. The first time it'll return you a string (where the escape
characters have been decoded) and the second time it'll decode that string
import 'dart:convert';
void main() {
var a = r'''"{\"Response\" : {\"Responsecode\" : \"0\" , \"Response\"
: \"Success\"}}"''';
var b = json.decode(json.decode(a));
print(b['Response']['Responsecode']); // 0
print(b['Response']['Response']); // Success
}
Post by Abhishek Aggarwal
To decode API response string to JSON, *json.decode()* works fine.
This will parse a JSON string similar to
{ “Response” : {“Responsecode” : “1” , “Response” : “Success”}}
\”Success\”}}”
*json.decode() *won’t work. I searched but couldn’t find how to
unescape characters in a string. Please help
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google
Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/4d7bacd1-3364-4c14-8b1c-ce05170dcdb6%40dartlang.org
<https://groups.google.com/a/dartlang.org/d/msgid/misc/4d7bacd1-3364-4c14-8b1c-ce05170dcdb6%40dartlang.org?utm_medium=email&utm_source=footer>
.
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/CAAqCwRXDh4%2B%3DsUU%3DCE9tCnmK2vi6mXXT_u0P12n5Lzi7Hs%3Da%2BQ%40mail.gmail.com
<https://groups.google.com/a/dartlang.org/d/msgid/misc/CAAqCwRXDh4%2B%3DsUU%3DCE9tCnmK2vi6mXXT_u0P12n5Lzi7Hs%3Da%2BQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAMAjJaJACxYnWX-3s6ruNHLVpKgDZSdR%2BtM2We2AFW5uX3bR6A%40mail.gmail.com.
'Danny Tuppeny' via Dart Misc
2018-10-14 20:08:37 UTC
Permalink
Post by Abhishek Aggarwal
How can I convert this json array to list of a class (say Class Response
having string variables as Responsecode and Response)?
Does dart has something similar to java's gson?
The best option may depend on what type of project you're working on.
There's some info on the Flutter website (including an answer to the exact
question "Is there a GSON/Jackson/Moshi equivalent in Flutter?"):

https://flutter.io/json/

I don't have much experience with any of this myself (the few projects I
work on that do this have their own code-gen'd methods) but that's
definitely where I'd start. Others here may have more experience and be
able to offer some better advice (depending on whether your project is
web/Flutter/VM).

Hope this helps!
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAJJHVm6EDbkueo_tw5eYs4LiLxHyvsOHUi2L9MMeRROXEjm_Xg%40mail.gmail.com.
Continue reading on narkive:
Loading...