Special Symbol or Single Quote or apostrophe in JSON String in flutter
2,783
As @Richard Heap answer
http.Response response = await http.get('SOME URL',headers: {'Content- Type':'application/json'});
List<dynamic> responseJson = json.decode(utf8.decode(response.bodyBytes));
Author by
Farhana Naaz Ansari
Farhana... The only way of writing fewer bugs is writing less code.
Updated on December 10, 2022Comments
-
Farhana Naaz Ansari over 1 year
JSON
response String containing an apostropheThis is the key and value coming from the server.
"name": "men’s basketball wear with free product",
When I'm converting this
JSON
intopojo response
Future<Product>
returning me this when I print the string in the log.PRODUCT NAME
menâs basketball wear with free product
I have tried this solution but nothing is happening
replaceAll("'", "\'").replaceAll('"', "\'") replaceAll('"', '\\"')
I have tried in response class
Product.fromJsonMap(Map<String, dynamic> map): pid = map["pid"], aid = map["aid"], name = map["name"].replaceAll("'", "\'");
My HTTP request
http.Response res = await http.get(url);
-
Günter Zöchbauer over 5 yearsI guess you get this JSON from an HTTP request and it's an encoding issue. Check the content-type header of the response.
-
Farhana Naaz Ansari over 5 years@GünterZöchbauer this is coming from get service ` http.Response res = await http.get(url);` should I need to change at server side?
-
Günter Zöchbauer over 5 yearsTry with
"application/json; charset=utf-8"
and check what the content-type header of the response is -
Farhana Naaz Ansari over 5 years@GünterZöchbauer still returning the same thing.
-
Günter Zöchbauer over 5 yearsYou could first check the response headers as mentioned before
-
Farhana Naaz Ansari over 5 years@GünterZöchbauer Thanks I was not able to check and change response header, I will have to manage what I have and found the solution by Richard Heap answer.
-
Richard Heap over 5 yearsAs you figured out, setting a
content-type
on the request isn't going to make any difference. As is typical with aGET
you aren't actually sending any content to the server. You want the server to send its content back to you in UTF-8; in fact it already is - so there's nothing more you can do in the request to influence the response. The problem is that the server is forgetting to include the charset suffix with itscontent-type
. Dart has a slightly unusual (for this day and age) default of LATIN-1 if the suffix is omitted. Glad you fixed it.
-
-
tudorprodan over 3 yearsThis solution worked for me, thanks. Btw, no need for
Content-Type
header, just for consistency maybe you wantAccept
header withapplication/json;charset=utf-8
value.