REST API와 통신하는 범용 앱이 있습니다. REST에서 응답을 DataContractJsonSerializer으로 역 직렬화합니다.JSON 사전 deserialize
사전을 포함하는 응답에 문제가 있습니다. deserialize 할 필요가없는이 사전 - 클래스에는이 사전이 없습니다. Win RT에서는 작동하지만 Win Phone에서는 작동하지 않습니다. 이 오류 메시지가 표시됩니다.
The dictionary cannot be deserialized because the member 'key of dictionary' was found more than once in the input.
Win RT가 아닌 Win Phone에서 작동하는 이유를 알 수 없습니다.
는 // 편집 - 추가 샘플 JSON
{
"ResultType": 0,
"Message": "",
"Exception": null,
"Result": {
"property": 1,
"property2": 2,
"property3": "2015-01-31T13:56:43.5337609+01:00",
"GeneratedQuestions": {
"className": [
{
"innerProperty": 1,
"innerProperty2": 2,
"innerProperty3": "sample",
"innerProperty4": [
{
"prop1": 1,
"prop2": 2,
"prop3": 3,
"prop4": "sample text",
}
]
},
{
"innerProperty": 1,
"innerProperty2": 2,
"innerProperty3": "sample2",
"innerProperty4": []
},
{
"innerProperty": 1,
"innerProperty2": 2,
"innerProperty3": "sample3",
"innerProperty4": []
}
]
}
}
}
역 직렬화 JSON에 대한 코드
protected T DeserializeJson<T>(string json)
where T : class
{
T type;
DataContractJsonSerializer jsonSerialized = new DataContractJsonSerializer(typeof(T), new DataContractJsonSerializerSettings { DateTimeFormat = new System.Runtime.Serialization.DateTimeFormat("dd.mm.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) });
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
try
{
ms.Position = 0;
type = jsonSerialized.ReadObject(ms) as T;
}
catch
{
type = default(T);
}
}
return type;
}
모델의
단순화 된 버전 :
[DataContract]
public class SampleClass
{
[DataMember(Name = "ResultType")
public string ResultType { get; set; }
[DataMember(Name = "Message")
public string Message{ get; set; }
[DataMember(Name = "Exception")
public Exception Exception{ get; set; }
[DataMember(Name = "Result")
public ResultModel Result{ get; set; }
}
public class ResultModel
{
[DataMember(Name = "property")
public int Property { get; set; }
[DataMember(Name = "property2")
public int Property2 { get; set; }
[DataMember(Name = "property3")
public string Property3 { get; set; }
}
json을 표시하십시오. – khlr
JSON의 단순화 된 버전을 추가했습니다. ** GeneratedQuestions **에 문제가 있습니다. - 사전이고 필요하지 않습니다. – Flow
역 직렬화 할 모델을 ('T')에 추가로 게시 할 수 있습니까? – khlr