How can i parse this Json:
[{
"value": "",
"date": {
"value": "17/04/2015"
},
"hour": {
"value": "21:00"
},
"venue": {
"value": "some stadium."
}}, {
"value": "",
"date": {
"value": "18/04/2015"
},
"hour": {
"value": "20:30"
},
"venue": {
"value": "some stadium"
}}]
I tried this code:
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
foreach (var item in serializer.Serialize(item.dates))
{
@* item == "34 ''" *@
}
but its return string "34 ''" instead what i want.
can someone help me to understand what i missed? Thanks
You should use the DeserializeObject
-Method instead of the Serialize
-Method.
It returns an object[] which contains Dictionary<string, object>
-Elements (your data)
To get the values from the array, cast the current Item to a Dictonary<string, object>
(represents a json object) and access the value with the indexer.
Example:
var v = new JavaScriptSerializer();
var arr = (object[]) v.DeserializeObject(File.ReadAllText("json.txt"));
for (var i = 0; i < arr.Length; i++)
{
Console.WriteLine(((Dictionary<string, object>)arr[i])["value"]);
}
Example 2 - Access the value inside of the date object:
var v = new JavaScriptSerializer();
var arr = (object[])v.DeserializeObject(File.ReadAllText("json.txt"));
for (var i = 0; i < arr.Length; i++)
{
// The current object
var c = (Dictionary<string, object>) arr[i];
// Getting the date object
var date = (Dictionary<string, object>) c["date"];
// Printing the value inside of the date object
Console.WriteLine(date["value"]);
// OUTPUT:
// 17/04/2015
// 18/04/2015
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.