3

I've been going around in circles on this problem where the JSON UTF-8 strings returned from a server contain unicode pairs like this:

\u00c3\u00bc

which is being rendered as two individual characters. However, It should be rendered as a single character. According to a table I found at this link, here are some more examples:

0xc3,0xa0 agrave
0xc3,0xa1 aacute
0xc3,0xa2 acircumflex
0xc3,0xa3 atilde
0xc3,0xa4 adiaeresis
0xc3,0xa5 aring
0xc3,0xa6 ae
0xc3,0xa7 ccedilla
0xc3,0xa8 egrave
0xc3,0xa9 eacute
0xc3,0xaa ecircumflex
0xc3,0xab ediaeresis
0xc3,0xac igrave
0xc3,0xad iacute
0xc3,0xae icircumflex
0xc3,0xaf idiaeresis
0xc3,0xb0 eth
0xc3,0xb1 ntilde
0xc3,0xb2 ograve
0xc3,0xb3 oacute

(Every case where I see this in my data would convert to an appropriate single character.)

Many of these apparently are 'aliases' of singlet forms like '\uxxxx', but I receive them this way as doublets. The raw data bytes show that this is actually how it is transmitted from the server.

(Once I have received them in UTF-8, there is no reason for me to keep them that way in local representation in memory.)

I don't know what to call this, so I'm having difficulty finding much information on it and I'm not able to communicate clearly on the subject. I would like to know why it's used and where I can find code that will convert it to something that my UIWebView can render correctly, but knowing what it's called is the point of my question.

My question then is what is this doublet or paired form called?

(If it's helpful, I am working in Objective-C and CocoaTouch.)

4

2 に答える 2

7
\u00c3\u00bc

これは 2 つの個別のキャラクターとしてレンダリングされます。

それは明示的に 2 つの文字を意味しüます。が表示されると予想してüいた場合は、JSON ジェネレーターまたはそれに供給される入力のいずれかで、さらに上流で誤った処理が行われています。誰かが一連のバイトを ISO-8859-1 としてデコードしましたが、UTF-8 を使用する必要がありました。

この問題は、JSON を読み取り、ISO-8859-1 にエンコードしてから UTF-8 にデコードすることで回避できます。しかし、これは実際の正しい入力を台無しにし、例から「間違った」文字セットが実際に ISO-8859-1 なのか Windows コード ページ 1252 なのかを判断することは不可能です。どちらかである可能性があります。

ただし、問題を回避しようとするのではなく、問題の原因を修正する必要があります。JSON を生成するのはあなたのサーバーですか? データはどこから来たのですか?\u00c3\u00bc意味することüは明らかに間違っているからです。

于 2012-05-04T11:37:27.030 に答える
5

この表記'\u00c3\u00bc'は、通常の JavaScript エスケープ表記を使用して、2 文字のシーケンス「ü」を示します。文字列リテラル内では'\uhhhh'、16 進数の Unicode 番号 hhhh を持つ文字 (または、技術的には Unicode コード単位) を表します。

これは、文字データ変換エラーのほぼ確実な兆候です。このようなエラーは、UTF-8 でエンコードされたデータが ISO-8859-1 でエンコードされたもの (または他の 8 ビット エンコード) として誤って解釈された場合に頻繁に発生します。

おそらく、実際の破損していないデータには、ウムラウト付きの u、ü、U+00FC が含まれており、UTF-8 エンコーディングはバイト c3 と bc で構成されています 。http://www.fileformat.info/info/unicode/char/fc/を参照してください。 index.htm

あなたが参照しているドキュメント、http://cpansearch.perl.org/src/JANPAZ/Cstools-3.42/Cz/Cstocs/enc/utf8.encは、テキスト形式で提示されたUTF-8でエンコードされた文字表現を示しているようですバイトを 16 進数で表示します。

于 2012-05-04T11:32:39.187 に答える