Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Part 11: Working with JSON Data

Download

Right click “Save as…”

In this lesson we talk about the JSON data format and look at the code in the RecipeDataSource.cs that loads and parses through that data to create instances of our RecipeDataItem and RecipeDataCollection objects.
 
Resources
 
 

Tags:

Follow the Discussion

  • wow!, stop being cool and change the background of this video.
    Viewing this online is terrible. The distraction is unbelievable. Keep it simple man, go regular next time. I can't follow your coolness here at all.

    Thanks anyway

  • What do you mean by "background" and "cool"?  Are you attempting to stream the video and you're only seeing garbled text?  If so please DOWNLOAD and view the video locally.  The video was created in high def 1280x720 and you may not have sufficient bandwidth to stream the video.  Silverlight throttles your playback experience based on the speed on your Internet connection.  If that's NOT what you're talking about you'll need to be more explicit in exactly what you're referring to so I can help.

  • i just downloaded (high quality mp4) this it works perfect for me, the font colors and background is awesome, loooveeee vs 2012 !!! (we shud rename it to vs 2013 its not fair we are in 2013 so quickly).

  • Jason ShortJason Short

    I don't know why this code was written with all these switch statements. Why not just deserialize the json into a class? This seems much more tightly coupled, you could have just used CSV files.

  • @Jason Short: Hi Jason, I only offer two replies to your question / comment ... (1) I think the authors of the Hands On Labs wanted to re-use the JSON file for C# and JavaScript versions.  So, rather than have the JavaScript version access a CSV, they chose to have the C# version access a JSON file.  (2) There are ways to map JSON objects and properties to C# classes and properties, but the most popular way I found was to use an open source library, JSON.NET ... take a look at this:

    http://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object

    So, I suspect that the HOL authors made a choice ... do they introduce a new library into the mix and require people to learn enough of it to be productive, OR do they just power through and parse the JSON manually.  They chose the latter.   Again, I'm just assuming this was their thought process.  Thank you!

     

    Edit: later that day ... After snooping around a little more, there's another option for JSON serialization, although I didn't really try it myself and don't know if it would work in a Windows Store app ... 

    http://blogs.microsoft.co.il/blogs/pini_dayan/archive/2009/03/12/convert-objects-to-json-in-c-using-javascriptserializer.aspx

  • Wow I understood your fundamental C# series perfectly, but I am finding this series very difficult. 

    I would have liked it if you started a project from scratch building all the classes and interfaces to get a deep understanding of what is going on, as your teaching methods are second to none so far when comparing to Pluralsight and TekPub tutorials. But I understand you may be a very busy man to do such a series. 

  • @ah07171: Thanks for the feedback ... admittedly, because there was an end goal in mind (i.e., the creation of a real app) this got off the ground much faster than the other series.  At what point did you start running into some friction?  Are you also using the Hands On Lab text or just the videos?

    Since you commented on this video, I'm going to assume this was the straw that broke the camel's back, so to speak.  There was a lot of complexity around working with the JSON and that accounted for the majority of the code.  The JavaScript version of this video was literally one line of code, so this was a trade off -- the team that created the Hands On Lab wanted to re-use the JSON data necessitating a lot of parsing and massaging of the data.

    Here's hoping you'll not get discouraged and will continue on with the series.

  • , BobTabor wrote

    @ah07171: Thanks for the feedback ... admittedly, because there was an end goal in mind (i.e., the creation of a real app) this got off the ground much faster than the other series.  At what point did you start running into some friction?  Are you also using the Hands On Lab text or just the videos?

    Since you commented on this video, I'm going to assume this was the straw that broke the camel's back, so to speak.  There was a lot of complexity around working with the JSON and that accounted for the majority of the code.  The JavaScript version of this video was literally one line of code, so this was a trade off -- the team that created the Hands On Lab wanted to re-use the JSON data necessitating a lot of parsing and massaging of the data.

    Here's hoping you'll not get discouraged and will continue on with the series.

     

    Thanks for the reply.

    It was actually some of the previous videos which I got stuck with, the classes, the interfaces some methods...etc, I do understand I have to really break it down and struggle with it.

    I also do understand going into detail with everything would have taken a lot more time and that hoping for a detailed tutorial starting from scratch on a project where the end goal is an app of this complexity was going to be too much. Also this project did require the use of a template.

    However I think I may just start on my own project soon, build something big from scratch something that I may need, instead of hoping to find someone building their own project and making a tutorial on it from scratch, like I have been hoping for hahaha.

    Anyway I will finish this series hoping it makes sense to me at some point, and I'm sure I will pick up a few new things along the way. 

    I look forward to your next series. 

  • reeganreegan

    how to display json string to table formate usin c#?

  • @reegan: Check this out ... it's specifically for WIndows Phone, HOWEVER since it's XAML it will be similar.  

    http://stackoverflow.com/questions/14153659/how-can-i-bind-data-into-xaml-windows-phone

    Hope that helps!

  • RaghuramanRaghuraman

    Hi,
    If I try to parse a correct json file it is working fine, at the same time if I parse an invalid json file it throws an Exception and I am able to handle it. However If I have a json file which is partially correct it doesn't throw an exception and It shows a blank screen. Could you please help me fix this bug.

  • @Raghuraman: Sorry for the delayed response.  You will need to add more checks when writing and reading.  When writing, ensure the JSON is correct.  If it's not, don't write it to the JSON file.  When reading, check the document first to ensure it's correct.  It takes a lot of code and a lot of testing to make something completely bullet proof.  If you give me more specifics of the problem, I might be able to suggest other things.

  • MahwishMahwish

    i am using grid template for window store app development in which all data is bind with SampleDataSource.cs . How we can implement json in that. Means how we will bind it with our ROOT Object class and retrieve result from it .

  • @Mahwish: Hi, I think you'll find the answer to that in lessons 13, 14 and 15.  Also, I had to stare at that class for a long time to really understand the relationships between the shape of the data, how it was deserialized into an object graph, and how that object graph was bound to.  I recommend you spend a few hours just really looking at each line of code in the SampleDataSource.cs to see those connections so that you can begin to imagine how to tweak it for your own needs.  That's the only way to really learn what's going on.  Hope that helps!

    i am using grid template for window store app development in which all data is bind with SampleDataSource.cs . How we can implement json in that. Means how we will bind it with our ROOT Object class and retrieve result from it .

  • Opening comments

  • Hi Bob,

    I tried to implement you code on my Windows Store Apps.I have used Newtonsoft.Json namespace in my application. I was able to parse json data into JArray. But i was not able to call the method item.GetObject() as you have shown in the video.

    Here is my code that i have written.How can i Implement item.GetObject inside foreach loop so that i can bind the object in the ListviewItem.

    Uri dataUri = new Uri("ms-appx:///Assets/team.txt");
                StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
                string Value = await FileIO.ReadTextAsync(file);
                JArray array = JArray.Parse(Value);

                foreach (var item in array)
                {
                     
                    string name = item.ToString();
                }

  • QamarQamar

    Sir how to connect Json with C# for window store application plese help me

  • Paarth ThadaniPaarth Thadani

    the " Download the entire series' source code " when download shows that this archive is corrupt . kindly rectify it

  • Greg Duncangduncan411 It's amazing what a professional photographer can do...

    @Paarth Thadani:Can you try it again? I just downloaded it and it downloaded and unzipped with no problems

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.