Привет! Я использую DataContractJsonSerializer для сериализации Json, которая выглядит следующим образом
{
"meta" : {
"code" : 200
},
"response" : {
"venues" : [{
"id" : "4eeb4c51775b1d4812e0261c",
"name" : "Shake Shack",
"contact" : {
"phone" : "7183077590",
"formattedPhone" : "(718) 307-7590"
},
"location" : {
"address" : "409 Fulton St.",
"crossStreet" : "at Boerum Pl.",
"lat" : 40.692143731043416,
"lng" : -73.98904329061085,
"distance" : 1272,
"postalCode" : "11201",
"city" : "Brooklyn",
"state" : "NY",
"country" : "United States"
},
"categories" : [{
"id" : "4bf58dd8d48988d16c941735",
"name" : "Burger Joint",
"pluralName" : "Burger Joints",
"shortName" : "Burgers",
"icon" : {
"prefix" : "https:\/\/foursquare.com\/img\/categories\/food\/burger_",
"sizes" : [32, 44, 64, 88, 256],
"name" : ".png"
},
"primary" : true
}
],
"verified" : false,
"stats" : {
"checkinsCount" : 2812,
"usersCount" : 1945,
"tipCount" : 38
},
"url" : "http:\/\/shakeshack.com",
"specials" : {
"count" : 0,
"items" : []
},
"hereNow" : {
"count" : 0
}
}, {
"id" : "49e644c6f964a5202f641fe3",
"name" : "Starbucks",
"contact" : {
"phone" : "2122192961",
"formattedPhone" : "(212) 219-2961",
"twitter" : "Starbucks"
},
"location" : {
"address" : "72 Spring St.",
"crossStreet" : "at Crosby St.",
"lat" : 40.722642,
"lng" : -73.997989,
"distance" : 2526,
"postalCode" : "10012",
"city" : "New York",
"state" : "NY",
"country" : "United States"
},
"categories" : [{
"id" : "4bf58dd8d48988d1e0931735",
"name" : "Coffee Shop",
"pluralName" : "Coffee Shops",
"shortName" : "Coffee Shop",
"icon" : {
"prefix" : "https:\/\/foursquare.com\/img\/categories\/food\/coffeeshop_",
"sizes" : [32, 44, 64, 88, 256],
"name" : ".png"
},
"primary" : true
}
],
"verified" : true,
"stats" : {
"checkinsCount" : 15042,
"usersCount" : 6472,
"tipCount" : 73
},
"specials" : {
"count" : 0,
"items" : []
},
"hereNow" : {
"count" : 0
}
}
]
}
}
Мой код выглядит следующим образом
Stream stream = new MemoryStream(Encoding.Unicode.GetBytes(e.Result));
DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(Foursq));
Foursq result = obj.ReadObject(stream) as Foursq;
Я получаю ArgumentException при попытке чтения объекта. Если классы, которые я создаю, не совпадают, результат должен быть нулевым, верно? Почему я получаю ArgumentException? Трудно ли разобрать мой json? Спасибо за помощь.
Редактировать: Ниже приведена трассировка стека.
System.ArgumentException was unhandled
Message=ArgumentException
StackTrace:
at System.Reflection.FieldInfo.SetValue(Object obj, Object value)
at System.Runtime.Serialization.Json.ObjectToDataContractConverter.SetMemberValue(Object newInstance, Object value, MemberInfo memInfo)
at System.Runtime.Serialization.Json.ObjectToDataContractConverter.ReadClassDataContractMembers(DataContractJsonSerializer serializer, ClassDataContract dataContract, Dictionary`2 deserialzedValue, Object newInstance, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.ObjectToDataContractConverter.ConvertDictionaryToClassDataContract(DataContractJsonSerializer serializer, ClassDataContract dataContract, Dictionary`2 deserialzedValue, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ConvertObjectToDataContract(DataContract contract, Object value, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.ObjectToDataContractConverter.ConvertICollectionToCollectionDataContract(DataContractJsonSerializer serializer, CollectionDataContract contract, Object deserializedValue, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ConvertObjectToDataContract(DataContract contract, Object value, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.ObjectToDataContractConverter.ReadClassDataContractMembers(DataContractJsonSerializer serializer, ClassDataContract dataContract, Dictionary`2 deserialzedValue, Object newInstance, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.ObjectToDataContractConverter.ConvertDictionaryToClassDataContract(DataContractJsonSerializer serializer, ClassDataContract dataContract, Dictionary`2 deserialzedValue, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ConvertObjectToDataContract(DataContract contract, Object value, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.ObjectToDataContractConverter.ReadClassDataContractMembers(DataContractJsonSerializer serializer, ClassDataContract dataContract, Dictionary`2 deserialzedValue, Object newInstance, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.ObjectToDataContractConverter.ConvertDictionaryToClassDataContract(DataContractJsonSerializer serializer, ClassDataContract dataContract, Dictionary`2 deserialzedValue, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ConvertObjectToDataContract(DataContract contract, Object value, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(Stream stream)
at PanoramaApp1.MainPage.wc_DownloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e)
at System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e)
at System.Net.WebClient.DownloadStringOperationCompleted(Object arg)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Delegate.DynamicInvokeOne(Object[] args)
at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)