This is my first C# code so it will not be a very ideal piece of code but will be of help to those who are trying to parse a JSON feed from GW on windows mobile.
To start with I have a simple screen with a "Get Data" button, On clicking this button a http get call is made to the GW and a JSON response is received. Subsequently this is parsed and printed on the console. I am not very comfortable with screen designing as yet so not venturing into that as of now.
Along with the above I will put in some titbits which might be of help for anyone who is coding in Visual Studio and C# for the first time like myself.
I am using the following environment
Microsoft Visual Studio 2008
Version 9.0.30729.1 SP
Microsoft .NET Framework
Version 3.5 SP1
Windows Mobile 6 Professional SDK
Windows Mobile 6 Classic Emulator
Before starting the actual coding there are some prerequisites that need to be done with.
- Setting up the emulator to access the network on your laptop.
To access GW we need to setup the emulator to use the network on the computer.
The steps for this are well explained in the below link.
- Providing a storage card for the emulator.
On the main window of the studio go to Tools -> Options ->Device Tools ->Devices
On the right side under Devices select the right Emulator and click Properties.
Then click Emulator Options.
Here, under the General tab, provide a folder path for "Shared folder". (Ex: c:\devicemem)
Henceforth this location will act as the storage card for the emulator. Any io action can be done on this location from emulator as well as laptop. If a change is made in a file from the laptop, a running emulator will also see the change. Thus its not required to restart the emulator to see the change.
- Getting Json.Net
Go to http://json.codeplex.com/click the Downloads tab on the top. Depending on the sdk and visual studio version you are using, download the best suitable library for use.
For my environment i downloaded Json.NET 3.5 Release 8
- Adding Json.Net to the project.
Adding a reference to the project is very simple. In the "Solution Manager" tab on the right side of the project window, right click on the References node and select Add Reference. Now browse to the location where you have downloaded and unzipped the library and select the proper dll. In my case its under "Compact" as mentioned by the Readme.txt present in the zip.
- Json.NET Documentation.
In the above URL there are 2 sections which were refered for the scope of this blog. They are "CustomCreationConverter" & "Serializing Collections" under "Serializing and Deserializing JSON".
- Simple ways of seeing output/debugging
To print the output on the "Immediate window"
To Show the output in a dialoug box.
Project creation and First screen
To start coding please follow the below mentioned steps:
- Create a Smart Device Project, it can be found under File -> New Project -> Visual C# -> Smart Device -> Smart Device Project.
- Next, add a button and change its label to "Get Data".
- Double click on the button and start coding as mentioned in the next section.
Making a HTTP Get call to GW and reading the response stream.
wrGETURL = WebRequest.Create(sURL);
wrGETURL.Headers.Add("Authorization: Basic n3VwdXNlcjI6czNwdXNlcg==");//Please provide proper Authorization
objStream = wrGETURL.GetResponse().GetResponseStream();
StreamReader reader = new StreamReader(objStream);
string text = reader.ReadToEnd();
Reading JSON from file.
Sometimes its easier to store the feed in a local file and work with it, in this way one can keep changing the JSON stored in the file to get the right class structure required to parse the actual feed. Here I have stored the feed in a local file feed.txt in the c:\devicemem folder ( see the above section for "Providing a storage card for the emulator"). This file is accessible from the emulator.
System.IO.StreamReader myFile =
new System.IO.StreamReader("\\Storage Card\\feed.txt");
string text = myFile.ReadToEnd();
Map classes to Travel Agency collection.
We will need to create 4 classes here:
You can find the class structure in the attached Collection.cs.txt
Its just one line of code as listed below
TravelagencyCollection tc = JsonConvert.DeserializeObject<TravelagencyCollection>(text); //for "text" see above
Accessing the parsed data.
Hope this helps!
- Collection.cs.txt.zip (456 bytes)