Reputation: 21
I'm parsing a html page in my flutter app, and somewhere in the middle of that html source has a json string in utf-8 format ( "\x" format).
I'm able to get the html content and then parse to it extract that json object in "\x" utf-8 format to a String var, but I'm not able to convert it to a json to decode it.
I tried printing the ranes of that first 4 letters in that parsed output "\x5B" it printing as 4 separate ints, while the same "\x5B" I statically assigned to a String var and printed the ranes , it only shows one digit. So just wondering how can I decode that extracted String in "\x" format ?
An extract of the code as below:
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
var res = utf8.decode(response.bodyBytes);
//gives the starting index of json object in html source
int startIndex = res.indexOf('var statData');
// start and end index of json object in "\x" format
int start = res.indexOf("(", startIndex) + 2;
int end = res.indexOf(");", start) - 1;
//extract the json in \x encoded
String dataJson = res.substring(start,end);
//now sample code to compare the string, one statically assigned,
//another extracted from the html source, to describe the issue I'm having now.
String sample1 = dataJson.substring(0,4)); //extracts "\x5B" from the string
String sample2 = "\x5B";
print(sample2.runes); // prints (91)
print(sample1.ranes); // prints (92, 120, 53, 66), expectation is to get (91)
}
Output :
I/flutter ( 3437): (91) I/flutter ( 3437): (92, 120, 53, 66)
While sample2.runes prints the single character (91)( equivalent ascii is '{' - start of the json)),
The same "\x5B" I extracted from the string not getting decoded as (91), instead it is treated as 4 separate characters, so looks like '\x' extracted string is not treated as utf-8 encode indicator.
I want the sample1.runes also to be {91}, how to approach this ?, where am I going wrong?
Upvotes: 1
Views: 1453
Reputation: 21
I don't know whether this is the correct way to handle it, but thanks to this post https://stackoverflow.com/a/64699290/17301137 , I was able to resolve it.
Basically It does a regex match of all Hex values after '\x' then replace it to corresponding string return.
final Pattern unicodePattern = new RegExp(r'\\x([0-9A-Fa-f]{2})');
final String newStr = sample1.replaceAllMapped(unicodePattern, (Match unicodeMatch) {
final int hexCode = int.parse(unicodeMatch.group(1)!, radix: 16);
final unicode = String.fromCharCode(hexCode);
return unicode;
});
this is the way I resolved it.
But not sure, whether there any better way to resolve it.
Upvotes: 1