3

I need to support UTF-8 in my MonoTouch iPhone app and have just updated all my server PHP scripts to be encoded in UTF-8 instead of ANSI.

This change has broken my client code and an exception is thrown in ServiceStack when trying to serialize the returned string from the PHP scripts.

using (StreamReader reader = new StreamReader (webResponse.GetResponseStream (), Encoding.UTF8))
{
    string responseString = reader.ReadToEnd();

    myDto = JsonSerializer.DeserializeFromString (responseString, dtoType);
}

Exception: System.Runtime.Serialization.SerializationException: Type definitions should start with a '{', expecting serialized type 'UserGamesDTO', got string starting with: {"uc":"0","gma":[{"gid":"838","ui

The exception is complaining that the string doesn't start with a '{' when it clearly does, so does this mean ServiceStack can't work with UTF-8 strings, or do I need to set something?

Thanks.

4

2 回答 2

2

您是否考虑过字节顺序标记 (BOM) 字符,这是文本编辑器中未显示的 Unicode 字符。也许你应该摆脱它。

于 2013-08-11T12:09:10.907 回答
0

请使用 json.net 库

var cfg = new ConfigFile();
cfg.xxx = xxx;
....
var str = Newtonsoft.Json.JsonConvert.SerializeObject(cfg);


var str = File.ReadAllText (filename, System.Text.Encoding.UTF8);
if (! String.IsNullOrEmpty (str)) {
    var cfg = Newtonsoft.Json.JsonConvert.DeserializeObject<ConfigFile> (str);
}
于 2013-08-11T15:28:04.837 回答