Home > Blockchain >  Dart: Incorrect interpretation of JSON String
Dart: Incorrect interpretation of JSON String

Time:05-01

I'm trying to decode a zipped json file that is dropped (drag and drop file) into my Flutter web-app, however am having some challenges getting the correct text from the file.

For example, the file contains the following line: "Cond\u00c3\u00a9 Nast"

This should be Condé Nast. I am somewhat aware of character encoding, but this has me stumped.

This is currently how I'm unzipping, utf8 decoding and finally json decoding the file. I am using the Archive package to do the unzipping.

ArchiveFile theFile = otherFiles.first;
final fileString = Utf8Decoder().convert(theFile.content);
Iterable l = json.decode(fileString);

How would I go about printing the correct character from this input JSON file string? Is this an issue of incorrect encoding? Or Is it an issue with my implementation?

CodePudding user response:

As discussed in comments, I suspect that theFile.content was incorrectly encoded at some earlier step and that it was sent through a UTF-8 encoder twice. You can verify that by opening the file in a hex editor and examining its bytes. Alternatively, in Dart you can do print(theFile.content); or, if you prefer seeing byte values in hexadecimal, print([for (var byte in theFile.content) byte.toRadixString(16)]);.

If you don't have any control over whatever generated that file, you should complain to someone who does. In the meantime, you can try to undo the damage by feeding your content through a UTF-8 decoder twice. utf.decode/Utf8Decoder().convert expect a List<int>, so you can't just feed them the mis-encoded String directly.

void main() {
  var badString = 'Cond\u00c3\u00a9 Nast';
  var goodString = utf8.decode(badString.runes.toList());
  print(goodString); // Prints: Condé Nast
}
  • Related