0
votes

Consider this 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"
},

etc..

Closely following this: http://blog.clauskonrad.net/2010/11/wp7-how-to-consume-json-data-from.html

My class is:

[DataContract]
public class ReturnedDetails
{
    [DataMember(Name="formattedAddress")]
    public string formattedAddress { get; set; }

}

And the event code:

    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);
        }
    }

When I run this, an InvalidCastException is thrown at ReturnedDetails[] obj = (ReturnedDetails[])ser.ReadObject(ms);

When I debug and hover over ser, KnownDataContracts is 'Could not evaluate expression' and 'null'.

I just want to get the value from the formattedAddress in the JSON, anyone know how?

Thanks for any help.

The stack trace is:

at PhoneApp1.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)

1

1 Answers

2
votes

The easiest way to do it is to have classes for the entire JSON. I use JSON 2 C# to write the boilerplate for this. It will give you some RootObject class that looks at the JSON as a whole.

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

From there you can go through your RootObject will hold your ReturnedDetails[].