views:

112

answers:

2

I'm developing my first windows phone 7 app, and I've hit a snag. basically it's just reading a json string of events and binding that to a list (using the list app starting point)

public void Load()
{
    // form the URI
    UriBuilder uri = new UriBuilder("http://mysite.com/events.json");

        WebClient proxy = new WebClient();  
    proxy.OpenReadCompleted += new OpenReadCompletedEventHandler(OnReadCompleted);  
    proxy.OpenReadAsync(uri.Uri);  
}

void OnReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    if (e.Error == null)
    {
        var serializer = new DataContractJsonSerializer(typeof(EventList));
        var events = (EventList)serializer.ReadObject(e.Result);
        foreach (var ev in events)
        {
            Items.Add(ev);
        }
    }
}

public ObservableCollection<EventDetails> Items { get; private set; }

EventDetails is my class that wraps the json string. this class has to be correct because it is an exact copy of the class used by that website internally from which the json is generated...

I get the json string correctly from the webclient call (I read the memorystream and the json is indeed there) but as soon as I attempt to deserialize the string, the application exits and the debugger stops.

I get no error message or any indication that anything happen, it just stops. This happens if I type the deserialize method into the watch window as well...

I have already tried using JSON.net in fact I thought maybe it was a problem with JSON.net so I converted it to use the native deserializer in the .net framework but the error is the same either way.

why would the application just quit? shouldn't it give me SOME kind of error message?

what could I be doing wrong?

many thanks!

+1  A: 

Firstly, the fact that you have some string there that looks like JSON does not mean that you have a valid JSON. Try converting a simple one.

If your JSON is valid, it might be that your JSON implementation does not know how to convert a list to EventList. Give it a try with ArrayList instead and let me know.

thelost
thanks for your reply! I'm confident that the JSON is valid because it's the same json used within the website to deserialize into the same eventdetails class. but I believe you're right it has to be something to do with the mapping because if I manually do it, creating a EventList class and parsing each value in the json it works and doesn't crash...but I don't like that it just shuts down. that's not helpful to anyone!! thanks again
Josh
A: 

I ran into this same kind of problem when trying to migrate some existing WM code to run on WP7. I believe that the WP7 app crashes whenever it loads an assembly (or class?) that references something that's not available in WP7. In my case, I think it was Assembly.Load or something in the System.IO namespace, related to file access via paths.

While your case might be something completely different, the symptoms were exactly the same.

The only thing I can recommend is to go through the JSON library and see if it's referencing base classes that are not allowed in WP7. Note that it doesn't even have to hit the line of code that's causing the issue - it'll crash as soon as it tries to hit the class that contains the bad reference.

If you can step into the JSON library, you can get a better idea of which class is causing the problem, because as soon as the code references it, the whole app will crash and the debugger will stop.

ZaijiaN