2013-08-11 2 views
3

MonoTouch iPhone 응용 프로그램에서 UTF-8을 지원해야하며 ANSI 대신 UTF-8로 인코딩되도록 모든 서버 PHP 스크립트를 업데이트해야합니다.ServiceStack JsonSerializer.DeserializeFromString이 UTF-8 문자열에서 작동하지 않습니다.

이 변경으로 인해 클라이언트 코드가 손상되고 PHP 스크립트에서 반환 된 문자열을 직렬화하려고 할 때 예외가 ServiceStack에서 발생합니다.

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 

는 예외 문자열로 시작하지 않는 불평하는 '{'가 명확하지 때, 그래서 이것은 ServiceStack이 UTF-8 문자열과 함께 작동하지 않을 수 있음을 의미, 또는 뭔가를 설정해야합니까 ?

감사합니다.

+0

입니다 json.net 라이브러리를 사용하십시오? – I4V

+0

안녕하세요 I4V, ContentEncoding이 비어 있습니다. 그래서 내 PHP 스크립트에서 UTF-8을 지정해야한다는 뜻입니까? 감사. – danfordham

답변

2

당신은 바이트 순서 마크 (BOM) charecter를 고려 했습니까? 이것은 텍스트 편집기에 표시되지 않은 유니 코드 charecter입니다. 어쩌면 당신은 그것을 제거해야합니다.

+0

이것은 문제처럼 보입니다, 감사합니다 아미르. 바이트 스트림을 검사하여 UTF-8의 BOM 인 바이트 239, 187 및 191로 시작한다는 것을 알았습니다. PHP 파일을 Notepad ++에서 열어서 UTF-8 (BOM없이)으로 변환 했으므로 이제 다시 작동합니다. – danfordham

0

`webResponse.ContentEncoding` 무엇

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); 
} 
+0

Newtonsoft를 사용해 보았지만 ServiceStack은 MonoTouch로 작업하기가 더 쉬웠습니다. 이 문제 외에도 아주 좋았습니다. – danfordham

관련 문제