dart json.encode(data) 不能接受其他语言 [英] dart json.encode(data) can not accept other language
问题描述
我目前正在使用 dart 进行 Web 开发.使用模拟客户端实现服务.但是,会发生以下错误.下面的实现代码是一个内存中的web api服务,它继承了mockClient.调用client.send()并返回结果的代码.
I am currently doing web development with dart. Implemented service with mockclient. However, the following error occurs. The implementation code below is an in memory web api service that inherits mockClient. The code that calls client.send () and returns the result.
test_value 是 json.encode(数据)的结果.
test_value is the result of 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
如果在上面的实现代码的名称中放了一个英文字符串,是没有错误的.为什么我在插入英文以外的字符时出现错误?
If you put an English string in the name of the above implementation code, there is no error. Why do I get an error when I insert a character other than English?
如果你知道请告诉我!
一个孤军奋战在韩国的飞镖程序员
A dart programmer struggling alone in Korea
推荐答案
Response
类对正文使用 Latin-1 编码,除非指定了其他内容.这在构造函数本身中没有明确记录,但 body
getter 上的文档确实表明了这一点.
The Response
class uses Latin-1 encoding for the body unless something else is specified.
This is not documented clearly on the constructor itself, but the documentation on the body
getter does suggest this.
尝试在标题中设置字符集/编码,例如:
Try setting the charset/encoding in the header, e.g., as:
return Response(test_value, 200, headers: {
HttpHeaders.contentTypeHeader: 'application/json; charset=utf-8'
});
这篇关于dart json.encode(data) 不能接受其他语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!