1

我有一个Delphi XE2Datasnap 服务器,我需要从数据库中接收一个带有换行符的字符串,并将其作为 a 重新发送JSON String到客户端应用程序。问题是我收到带有#10#13换行符的字符串,而我的客户期望\n换行符。这似乎是发送换行符的 JSON 默认方式,我希望转换是自动的,但似乎并非如此。

我正在考虑实施一种我自己的转换方法,但这将是最坏的情况,我想避免可能的“重新发明轮子”的情况。所以我的问题是,有没有更好的选择?

4

1 回答 1

3

#10 is a newline or line feed (LF) character, represented as \n in c-derived languages, and #13 is a carriage return (CR) represented as \r. The usual order in Windows and some internet protocols (e.g. SMTP and HTTP) is \r\n.

Unfortunately, different operating systems uses different line breaks (see also http://en.wikipedia.org/wiki/Newline)

Delphi (or the JSON framework you use) cannot guess your line break expectation. I am afraid you have to write extra code to make line breaks fit your client.

In reality, what is probably happening is that the strings are coming from a database or other source where they are stored with literal CR LF sequences. Delphi is just passing them on unchanged.

Before you do any work, make sure the client actually cares.

于 2012-06-14T04:07:45.337 回答