简体   繁体   English

Play框架中的Json响应字符编码

[英]Json response character encoding in Play Framework

I am having a problem and I need your help. 我有问题,需要您的帮助。

I am working in a server with Play Framework v1.2.4 in java that I use for my mobile app, and I have a problem in the Json response. 我正在使用用于移动应用程序的Java中具有Play Framework v1.2.4的服务器,并且在Json响应中有问题。

I have always use the views for enconding my Json response, but I have realized that when I use a special character (like ñ, \\n, á, é, etc) there is a problem and my mobile app have a wrong run. 我一直使用视图来表示我的Json响应,但是我意识到,当我使用特殊字符(如ñ,\\ n,á,é等)时,会出现问题,并且我的移动应用程序运行有误。

Here is the code: 这是代码:

{
  "id":${data[0]},

  "messages": 
  [
      #{list items:data[1], as:'message'}
      {           
           "text":"${message.text}"
      }${message_isLast ? '' : ','
}#{/list}
 ] }

I have tried to do it with a java method to parse the response correctly instead of in the .json view, and it is ok. 我尝试使用java方法来正确解析响应,而不是在.json视图中解析它,并且可以。 That is the code: 那是代码:

JsonObject jsonOut = new JsonObject(); 
jsonOut.addProperty("id",conversation.id); 
JsonArray jsonArray = new JsonArray();   

for(int i = 0; i < conversation.messages.size; i++)  {
    Message message = conversation.messages.get(i);
    JsonObject jsonMessage = new JsonObject();
    jsonMessage.addProperty("text", message.text);
    jsonArray.add(jsonMessage); 
}

jsonOut.add("messages", jsonArray); 

renderText(jsonOut);

My question is if I can do something in the view for parsing the variables correctly to json format without doing manually, because I have a lot of responses that I have to change. 我的问题是,我是否可以在视图中执行某些操作以将变量正确解析为json格式而无需手动进行,因为我必须更改很多响应。

Using templates for rendering JSON strings is... bad (and here I should just finish my answer, anyway...). 使用模板来呈现JSON字符串是很糟糕的(无论如何,我应该在这里完成答案)。

JSON serializers usually takes care about proper quotes, escapes special chars, honors types, encodes characters (if required) etc... As I can see your template DOESN'T do that. JSON序列化程序通常会注意正确的引号,转义特殊字符,荣誉类型,对字符进行编码(如果需要)等...据我所知,您的模板不会这样做。

With templates you need to do everything yourself, and if you don't know JSON specification perfectly, you are convicted to fail. 使用模板,您需要自己做所有事情,如果您不完全了解JSON规范,则定罪失败。 It's not just like pasting ${message.text} in proper place... 这不只是将${message.text}粘贴在适当的位置...

In other words: your second approach will be always better than first. 换句话说:您的第二种方法将永远比第一种更好。

The default encoding for JSON is UTF-8. JSON的默认编码为UTF-8。 The default encoding for Java is locale dependant. Java的默认编码取决于语言环境。

You need to make sure that the JVM you are running Play on is also running UTF-8 as Charset.defaultCharset(). 您需要确保运行Play的JVM还以Charset.defaultCharset()的形式运行UTF-8。

Both the compiler and the editor should have their encoding set to "UTF-8". 编译器和编辑器均应将其编码设置为“ UTF-8”。 The Play Framework and JSON use already UTF-8. Play框架和JSON已使用UTF-8。 Properties files should be \\u.... encoded. 属性文件应使用\\u....编码。 The error probably lies in non-Java files. 该错误可能出在非Java文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM