2011-12-13 5 views
0

내가 정의 JSON 디시리얼라이저를 쓰고 있어요 내가이 편지를 쓰고있어이없는 JSON의 직렬화 :사용자 정의 키

public class MyObjectToJson : JavaScriptConverter 
{ 
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) 
{ 
    MyObject TheObject = new MyObject; 

    TheObject.TheValue = serializer.ConvertToType<string>(dictionary["TheValue"]); 

가 무엇 JSON 개체가 키 TheValue을 포함하지 않는하면 어떻게됩니까? 이 코드가 충돌하거나 TheObject.TheValue = ""를 계속 사용합니까?

답변

0

예외를 throw합니다. dictionary["TheValue"]으로 전화하기 전에 dictionary.ContainsKey("TheValue")을 확인하여 피할 수 있습니다.

+0

좋습니다, 팁 주셔서 감사합니다. – frenchie

관련 문제