![](/img/trans.png)
[英]How do I just JavaScriptSerializer to deserialize JSON defined at runtime
[英]How do I Deserialize the following JSON string with the .NET JavaScriptSerializer class?
我有以下JSON對象:
[{"newValue":"{\"id\":\"1\",\"desc\":\"description\"}",
"oldValue":"{\"id\":\"2\",\"desc\":\"description2\"}"}]
newValue
包含要反序列化的對象的新值,而oldValue
包含要反序列化的對象的舊值,但是我不確定如何分別對每個對象反序列化,或者可以一起完成嗎?
您的json有點奇怪,因為newValue和oldValue的值是string ,而不是object。 看來它們是雙重序列化的。 下面的代碼有效(首先反序列化整個json字符串,然后反序列化old / new / values )
var jArr = JArray.Parse(json);
var anon = new { id = 0, desc = "" };
var items = jArr.Select(item => new
{
NewValue = JsonConvert.DeserializeAnonymousType(item["newValue"].ToString(),anon),
OldValue = JsonConvert.DeserializeAnonymousType(item["oldValue"].ToString(),anon)
})
.ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.