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.
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.
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.
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 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.
http://james.newtonking.com/projects/json/help/
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".
To print the output on the "Immediate window"
using System.Diagnostics;
Debug.WriteLine("Any text");
To Show the output in a dialoug box.
MessageBox.Show("Any Message");
To start coding please follow the below mentioned steps:
Making a HTTP Get call to GW and reading the response stream.
string sURL;
WebRequest wrGETURL;
wrGETURL = WebRequest.Create(sURL);
wrGETURL.Headers.Add("Authorization: Basic n3VwdXNlcjI6czNwdXNlcg==");//Please provide proper Authorization
Stream objStream;
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();
myFile.Close();
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
Parsing JSON.
Its just one line of code as listed below
using Newtonsoft.Json;
TravelagencyCollection tc = JsonConvert.DeserializeObject<TravelagencyCollection>(text); //for "text" see above
Accessing the parsed data.
Debug.WriteLine(tc.d.results[0].__metadata.type);
Debug.WriteLine(tc.d.results[1].NAME);
Hope this helps!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
38 | |
25 | |
17 | |
13 | |
7 | |
7 | |
7 | |
7 | |
6 | |
6 |