dart json.encode(data)不能接受其他语言

问题描述 投票:1回答:1

我目前正在使用dart进行Web开发。使用mockclient实现服务。但是,会发生以下错误。下面的实现代码是一个内存web api服务,它继承了mockClient。调用client.send()并返回结果的代码。

test_value是json.encode(data)的结果。

var test_value = '{"id": 1, "type": "Appetizer", "name": "한글"}';

     return Response (test_value, 200, headers: {'content-type': 'application / json'});

错误

Invalid argument(s): String contains invalid characters.
dart:convert                                           Latin1Codec.encode
package:http/src/response.dart 36:49                   new Response
package:basil/common/mock_rest/mock_recipe.dart 40:12  MockRecipe._handler

如果在上面的实现代码的名称中放入英文字符串,则没有错误。插入英语以外的字符时,为什么会出错?

如果你知道,请告诉我!

一个飞镖程序员独自在韩国挣扎

dart flutter angular-dart
1个回答
4
投票

除非指定了其他内容,否则Response类将使用Latin-1编码。这在构造函数本身没有明确记录,但body getter上的文档确实暗示了这一点。

尝试在标题中设置字符集/编码,例如:

return Response(test_value, 200, headers: {
    HttpHeaders.contentTypeHeader: 'application/json; charset=utf-8'
});
© www.soinside.com 2019 - 2024. All rights reserved.