Before going to start parsing json. You must
be add reference to both "system.runtime.serialization" and "system.servicemodel.web".
And in this sample i am using http://json2csharp.com/ for
simply building a C# class from JSON formated string. And its very important is
to make class as similar to Json objects. Other wise we will never parse date
properly.
This is time I am going to cover DataContract JSON Serialization using DataContractJsonSerializer. DataContract
JSON Serialization is mostly used by Windows Communication Foundation (WCF)
services, but I will try to apply this approach to Windows Phone 7
environment.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
using
Microsoft.Phone.Controls;
using
System.Runtime.Serialization.Json;
using
System.IO;
using
System.Text;
using
System.Runtime.Serialization;
namespace
Json_Parsing
{
public partial
class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
string jsonString =
@"{
'message': 'Success',
'status': '0',
'paramss': [
{
'redirect_url': 'https //www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=EC-87B5001D184562D',
'ack': '',
'transactionTimestamp': 'Thu May 12 06 45 57 EDT 2011'
},
{
'redirect_url': 'https //www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=EC-87B5001D184562D',
'ack': 'Success',
'transactionTimestamp': 'Thu May 12 06 45 57 EDT 2011'
},
{
'redirect_url': 'https //www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=EC-87B5001D184562D',
'ack': 'Success',
'transactionTimestamp': 'Thu May 12 06 45 57 EDT 2011'
},
{
'redirect_url': 'https //www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=EC-87B5001D184562D',
'ack': 'Success',
'transactionTimestamp': ''
},
{
'redirect_url': '',
'ack': 'Fail',
'transactionTimestamp': 'Thu May 12 06 45 57 EDT 2011'
},
{
'redirect_url': 'https //www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=EC-87B5001D184562D',
'ack': 'Success',
'transactionTimestamp': 'Thu May 12 06 45 57 EDT 2011'
}
]
}";
;
RootObject childlist =
new RootObject();
RootObject childlistonly =
new RootObject();
MemoryStream ms =
new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
DataContractJsonSerializer ser = new
DataContractJsonSerializer(childlist.GetType());
childlist =
ser.ReadObject(ms) as
RootObject;
string teststring =
"";
foreach (var d
in childlist.paramss)
{
teststring =
teststring + "" + d.redirect_url +
"\n" + "\n"
+ "\n";
}
MessageBox.Show("Your Url's are:\n\n"
+ teststring);
ms.Close();
}
}
public class
Paramss
{
public string
redirect_url { get; set;
}
public string
ack { get; set;
}
public string
transactionTimestamp { get; set; }
}
public class
RootObject
{
public string
message { get; set;
}
public string
status { get; set;
}
public List<Paramss>
paramss { get; set;
}
}
}