2015-01-31 2 views
0

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

json을 표시하십시오. – khlr

+0

JSON의 단순화 된 버전을 추가했습니다. ** GeneratedQuestions **에 문제가 있습니다. - 사전이고 필요하지 않습니다. – Flow

+0

역 직렬화 할 모델을 ('T')에 추가로 게시 할 수 있습니까? – khlr

답변

1

나는 오류가 [DataContract] 누락에있다 생각 -adribute on ResultModel 이것은 모든 publi를 직렬화하도록 serializer에 지시합니다. C 회원 :

Apply the IgnoreDataMemberAttribute attribute to opt-out of the default DataContractSerializer behavior. By default, the DataContractSerializer serializes all publicly visible types. All public read/write properties and fields of the type are serialized. You can change the default behavior by applying the DataContractAttribute and DataMemberAttribute attributes to the types and members

그래서 하나 ResultModel에 그 [DataContract] -attribute을 넣어 또는 [IgnoreDataMember] 같은 GeneratedQuestions를 표시합니다.

모델에 해당 속성이 없으므로 후자는 옵션이 아닙니다. 그 두 번째 옵션을 언급하기 만하면됩니다.

+0

[IgnoreDataMember]를 추가했지만 여전히 작동하지 않습니다. 추가 조언을 주셔서 감사합니다. – Flow

+0

그리고'[DataContract]는 어떨까요? – khlr

+0

[DataContract] 나는 또한 추가했다. 그러나 나는 나의 질문에 글쓰기를 잊었다 (지금 거기에있다) – Flow