I’m currently trying to deserialize the following JSON output in silverlight without javascript deserializer. I heard there’s a way to do this using JsonArray and LINQ, but I can’t quite figure it out.
{
"Security": {
"CIK": "0000789019",
"Cusip": "594918104",
"Symbol": "MSFT",
"ISIN": "US5949181045",
"Valoren": "951692",
"Name": "Microsoft Corporation",
"Market": "NASDAQGS",
"CategoryOrIndustry": "TECHNOLOGY",
"Outcome": "Success",
"Message": null,
"Identity": null,
"Delay": 0
},
"StartDate": "7/1/2011",
"EndDate": "7/6/2011",
"Quotes": [{
"Date": "7/5/2011",
"Last": 26.03,
"Open": 26.1,
"LastClose": 26.02,
"High": 26.15,
"Low": 25.9,
"ChangeFromOpen": -0.07,
"PercentChangeFromOpen": -0.268,
"ChangeFromLastClose": 0.01,
"PercentChangeFromLastClose": 0.038,
"Volume": 37803000,
"SplitRatio": 1,
"LastAdjusted": 26.03,
"OpenAdjusted": 26.1,
"LastCloseAdjusted": 26.02,
"HighAdjusted": 26.15,
"LowAdjusted": 25.9,
"ChangeFromOpenAdjusted": -0.07,
"ChangeFromLastCloseAdjusted": 0.01,
"VolumeAdjusted": 37803000,
"NotTraded": false,
"Outcome": "Success",
"Message": null,
"Identity": null,
"Delay": 0
}, {
"Date": "7/1/2011",
"Last": 26.02,
"Open": 25.93,
"LastClose": 26,
"High": 26.17,
"Low": 25.84,
"ChangeFromOpen": 0.09,
"PercentChangeFromOpen": 0.347,
"ChangeFromLastClose": 0.02,
"PercentChangeFromLastClose": 0.077,
"Volume": 52914500,
"SplitRatio": 1,
"LastAdjusted": 26.02,
"OpenAdjusted": 25.93,
"LastCloseAdjusted": 26,
"HighAdjusted": 26.17,
"LowAdjusted": 25.84,
"ChangeFromOpenAdjusted": 0.09,
"ChangeFromLastCloseAdjusted": 0.02,
"VolumeAdjusted": 52914500,
"NotTraded": false,
"Outcome": "Success",
"Message": null,
"Identity": null,
"Delay": 0
}],
"Outcome": "Success",
"Message": null,
"Identity": "Cookie",
"Delay": 0.014001
}
What would be the best approach to doing something like this where I’m trying to extract the Date and LastAdjusted from the Quotes array and put each into their own arrays?
Here’s how far I got to get the json into a stream:
downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(downloader_OpenReadCompleted);
downloader.OpenReadAsync(serviceUri);
}
void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
Stream responseStream = e.Result;
//add code here
}
}
The following code for Silverlight will take a JSON string, deserialize it to a “Listing” object and then loop through a list of quotes for the listing to extract the Date and LastAdjusted. Make sure to add a reference to System.ServiceModel.Web to your project in order to access the DataContractJsonSerializer (MSDN) class.
C#
Classes
You can learn more here: JSON serialization and deserialization in Silverlight