次の JSON を検討してください。
resourceSets: [
{
estimatedTotal: 5,
resources: [
{
__type: "Location:http://schemas.microsoft.com/search/local/ws/rest/v1",
bbox: [
51.3014406,
-8.3233626,
51.3037489,
-8.3182203
],
name: "Some Address",
point: {
type: "Point",
coordinates: [
51.3033847,
-2.3204335
]
},
address: {
addressLine: "SomeAddress",
adminDistrict: "MI",
adminDistrict2: "South Country",
countryRegion: "England",
formattedAddress: "Some Formattedaddress",
locality: "Derby",
postalCode: "12345"
},
等..
これに厳密に従う: http://blog.clauskonrad.net/2010/11/wp7-how-to-consume-json-data-from.html
私のクラスは:
[DataContract]
public class ReturnedDetails
{
[DataMember(Name="formattedAddress")]
public string formattedAddress { get; set; }
}
そしてイベントコード:
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
using (var ms = new system.IO.MemoryStream(Encoding.Unicode.GetBytes(e.Result)))
{
var ser = new DataContractJsonSerializer(typeof(ReturnedDetails[]));
ReturnedDetails[] obj = (ReturnedDetails[])ser.ReadObject(ms);
}
}
これを実行すると、InvalidCastException
がスローされますReturnedDetails[] obj = (ReturnedDetails[])ser.ReadObject(ms);
デバッグしてカーソルを合わせるとser
、KnownDataContracts
「式を評価できませんでした」と「null」です。
JSONのformattedAddressから値を取得したいだけですが、方法を知っている人はいますか?
助けてくれてありがとう。
スタック トレースは次のとおりです。
PhoneApp1.MainPage.wc_DownloadStringCompleted (オブジェクト送信者、DownloadStringCompletedEventArgs e) で System.Net.WebClient.OnDownloadStringCompleted (DownloadStringCompletedEventArgs e) で System.Net.WebClient.DownloadStringOperationCompleted (オブジェクト引数) で System.Reflection.RuntimeMethodInfo.InternalInvoke (RuntimeMethodInfortmi、オブジェクトSystem.Reflection.RuntimeMethodInfo.InternalInvoke(オブジェクト obj、BindingFlags invokeAttr、バインダー バインダー、オブジェクト [] パラメーター、CultureInfo Culture, StackCrawlMark& stackMark) で System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
System.Delegate.DynamicInvokeOne(Object[] args) で System.MulticastDelegate.DynamicInvokeImpl(Object[] args) で System.Delegate.DynamicInvoke(Object[] args) で System.Windows.Threading.DispatcherOperation.Invoke() で System.Windows.Threading.DispatcherOperation.Invoke() で.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority 優先度) で System.Windows.Threading.Dispatcher.OnInvoke(オブジェクト コンテキスト) で System.Windows.Hosting.CallbackCookie.Invoke(Object[] args) で System.Windows.Hosting.DelegateWrapper .InternalInvoke(Object[] args)
at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)