Json.NET
转自:http://james.newtonking.com/json
Serialize JSON
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };
string json = JsonConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "Expiry": "2008-12-28T00:00:00",
// "Sizes": [
// "Small"
// ]
//}
Deserialize JSON
string json = @"{
‘Name‘: ‘Bad Boys‘,
‘ReleaseDate‘: ‘1995-4-7T00:00:00‘,
‘Genres‘: [
‘Action‘,
‘Comedy‘
]
}";
Movie m = JsonConvert.DeserializeObject<Movie>(json);
string name = m.Name;
// Bad Boys
Validate JSON
JsonSchema schema = JsonSchema.Parse(@"{
‘type‘: ‘object‘,
‘properties‘: {
‘name‘: {‘type‘:‘string‘},
‘hobbies‘: {‘type‘: ‘array‘}
}
}");
JObject person = JObject.Parse(@"{
‘name‘: ‘James‘,
‘hobbies‘: [‘.NET‘, ‘LOLCATS‘]
}");
bool valid = person.IsValid(schema);
// true
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。