如何从wp7中的JSON获取数据

时间:2013-02-09 13:50:24

标签: c# .net json windows-phone-7

考虑这个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)at   System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs   e)在System.Net.WebClient.DownloadStringOperationCompleted(Object   arg)at   System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo   rtmi,Object obj,BindingFlags invokeAttr,Binder binder,Object   参数,CultureInfo文化,布尔isBinderDefault,汇编   调用者,布尔验证访问,StackCrawlMark& stackMark)at   System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,   BindingFlags invokeAttr,Binder binder,Object []参数,   CultureInfo culture,StackCrawlMark& stackMark)at   System.Reflection.MethodBase.Invoke(Object obj,Object []参数)
  在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   System.Windows.Threading.Dispatcher.OnInvoke(Object。的优先级)   System.Windows.Hosting.CallbackCookie.Invoke(Object []上下文)   args)at   System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object [] args)
  在System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr   pHandle,Int32 nParamCount,ScriptParam [] pParams,ScriptParam&   pResult)

1 个答案:

答案 0 :(得分:2)

最简单的方法是为整个JSON提供类。我使用JSON 2 C#为此编写样板文件。它会给你一些RootObject类来查看整个JSON。

WebResponse ws = req.GetResponse();
//Deserialize the JSON
DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(RootObject));
//Cast to root object
RootObject ro = (RootObject)ds.ReadObject(ws.GetResponseStream());

从那里,您可以查看RootObject将保留ReturnedDetails[]