Answered by:
A first chance exception of type 'System.InvalidCastException' occurred in PhoneApp4.dll

Question
-
Hi,
I was doing json parsing..But I was coming across an exception
A first chance exception of type 'System.InvalidCastException' occurred in PhoneApp4.dll.
public MainPage()
{
InitializeComponent();
if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
MessageBox.Show("No network connection available!");
return;
}
WebClient downloader = new WebClient();
Uri uri = new Uri("http://gdata.youtube.com/feeds/api/playlists/PLhhKzfJ3WyA3INKPYXqR94iCIDNwBKdNr/?v=2&alt=json&start-index=1&max-results=7&feature=plcp");
downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Downloaded);
downloader.DownloadStringAsync(uri);
}
void Downloaded(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Result == null || e.Error != null)
{
MessageBox.Show("There was an error downloading the file!");
}
else
{
string s = Convert.ToString(e.Result);
//IEnumerable
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IEnumerable<Item>));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(s));
var list = (IEnumerable<Item>)serializer.ReadObject(ms);List<Item> datasource = new List<Item>();foreach (Item item in list){datasource.Add(new Item{title =item.title});listBox1.ItemsSource = datasource;
}
}
}
}
public class Item
{
public string title { get; set; }
}
}
Exception was raising in this line var list = (IEnumerable<Item>)serializer.ReadObject(ms);
Can anybody please help me with this..Many Thanks....
- Edited by venukoti Thursday, February 12, 2015 11:28 AM
Thursday, February 12, 2015 11:26 AM
Answers
-
That is bit strange, can you try removing the SimpleJson and then add it again. Go to tools->Nuget Package Manager -> Manage Nuget Package for solution -> Installed packages - remove 'SimpleJson' and now again from package manager console add it.
http://developer.nokia.com/community/wiki/Using_Crypto%2B%2B_library_with_Windows_Phone_8
- Edited by Vineet24 Friday, February 13, 2015 9:54 AM
- Proposed as answer by Franklin ChenMicrosoft employee Monday, March 2, 2015 8:13 AM
- Marked as answer by Franklin ChenMicrosoft employee Tuesday, March 3, 2015 10:50 AM
Friday, February 13, 2015 9:54 AM -
Well you can take a observablecollection or simply a List to keep storing the id's or title to it and data bind that List in xaml page then.
http://developer.nokia.com/community/wiki/Using_Crypto%2B%2B_library_with_Windows_Phone_8
- Proposed as answer by Franklin ChenMicrosoft employee Monday, March 2, 2015 8:13 AM
- Marked as answer by Franklin ChenMicrosoft employee Tuesday, March 3, 2015 10:49 AM
Friday, February 13, 2015 11:53 AM
All replies
-
Your Item class does not appear to match the content of the JSON returned by that URL.
What do you get if you don't cast the result of ReadObject?
Eric Fleck, Windows Store and Windows Phone Developer Support. If you would like to provide feedback or suggestions for future improvements to the Windows Phone SDK please go to http://wpdev.uservoice.com/ where you can post your suggestions and/or cast your votes for existing suggestions.
Thursday, February 12, 2015 6:01 PM -
Thank you.That means url is not having Item class property...Friday, February 13, 2015 4:38 AM
-
JSON returned by the service is not an array , thereby an exception is occurring at the specific line( considering IEnumerable being one), so you need to cast it as object (basically the json returned is a tree of objects which needs to be parsed)
I tried parsing it with a much simpler json parser SimpleJson , here's a sample snippet :
void Downloaded(object sender, DownloadStringCompletedEventArgs e) { if (e.Result == null || e.Error != null) { MessageBox.Show("There was an error!"); } else { IDictionary<string, object> _GenericObj = null; IDictionary<string, object> _GenericObj1 = null; IDictionary<string, object> _GenericObj2 = null; _GenericObj = (IDictionary<string, object>)(SimpleJson.DeserializeObject(e.Result)); // all objects parsed _GenericObj1 = (IDictionary<string, object>)_GenericObj["feed"]; //root object 'feed' parsed _GenericObj2 = (IDictionary<string, object>)_GenericObj1["id"]; //'id' object parsed from 'feed' object // likewise you can parse the whole object tree(like done for 'id' , considering all objects as branches of 'feed' object)
http://developer.nokia.com/community/wiki/Using_Crypto%2B%2B_library_with_Windows_Phone_8
- Edited by Vineet24 Friday, February 13, 2015 5:48 AM
Friday, February 13, 2015 5:47 AM -
Hii.
I was getting error in Simplejson..
The name 'Simplejson' does not exist in the current context.
I executed (Install-Package SimpleJson) in Package Manager Console.I was also getting below message
'SimpleJson 0.38.0' already installed.
PhoneApp4 already has a reference to 'SimpleJson 0.38.0'.Even though i was comming across the same error.Please help me with this.
ManyThanks...
Friday, February 13, 2015 8:28 AM -
Is SimpleJson.cs class present in project, as soon as SimpleJson is installed from Nuget, the class gets included in the project itself.
http://developer.nokia.com/community/wiki/Using_Crypto%2B%2B_library_with_Windows_Phone_8
- Edited by Vineet24 Friday, February 13, 2015 9:08 AM
Friday, February 13, 2015 9:04 AM -
Ya, it was present eventhough same error was raising...
The name 'Simplejson' does not exist in the current context..
Friday, February 13, 2015 9:45 AM -
That is bit strange, can you try removing the SimpleJson and then add it again. Go to tools->Nuget Package Manager -> Manage Nuget Package for solution -> Installed packages - remove 'SimpleJson' and now again from package manager console add it.
http://developer.nokia.com/community/wiki/Using_Crypto%2B%2B_library_with_Windows_Phone_8
- Edited by Vineet24 Friday, February 13, 2015 9:54 AM
- Proposed as answer by Franklin ChenMicrosoft employee Monday, March 2, 2015 8:13 AM
- Marked as answer by Franklin ChenMicrosoft employee Tuesday, March 3, 2015 10:50 AM
Friday, February 13, 2015 9:54 AM -
Ya thankyou I removed and added it again it was working fine.
How can i bind them to listbox.
I tried like this
_GenericObj1 = (IDictionary<string, object>)_GenericObj["feed"]; //root object 'feed' parsed
_GenericObj2 = (IDictionary<string, object>)_GenericObj1["title"];
listBox1.ItemsSource = _GenericObj2;An exception was rising.
Thankyou..
- Edited by venukoti Friday, February 13, 2015 11:42 AM
Friday, February 13, 2015 11:41 AM -
Well you can take a observablecollection or simply a List to keep storing the id's or title to it and data bind that List in xaml page then.
http://developer.nokia.com/community/wiki/Using_Crypto%2B%2B_library_with_Windows_Phone_8
- Proposed as answer by Franklin ChenMicrosoft employee Monday, March 2, 2015 8:13 AM
- Marked as answer by Franklin ChenMicrosoft employee Tuesday, March 3, 2015 10:49 AM
Friday, February 13, 2015 11:53 AM