dart json.encode(data)无法接受其他语言 [英] dart json.encode(data) can not accept other language
问题描述
我目前正在使用dart进行Web开发. 使用嘲讽客户端实现服务. 但是,发生以下错误. 下面的实现代码是一个继承了mockClient的内存中Web api服务. 调用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?
如果您知道,请告诉我!
Please let me know if you know!
在韩国独自奋斗的飞镖程序员
A dart programmer struggling alone in Korea
推荐答案
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屋!