我是下面的JSON,
[
{
"document":
{
"createdDate":1476996267864,
"processedDate":1476996267864,
"taxYear":"2015",
"type":"user_document"
}
},
{
"document":
{
"createdDate":1476998303463,
"processedDate":0,
"taxYear":"2015",
"type":"user_document"
}
}
]
我需要将其转换为c#对象。我的对象类型如下 -
public class UserDocument
{
[JsonProperty(PropertyName = "type")]
public string type { get; set; }
[JsonProperty(PropertyName = "taxYear")]
public string taxYear { get; set; }
[JsonProperty(PropertyName = "createdDate")]
public string createdDate { get; set; }
[JsonProperty(PropertyName = "processedDate")]
public string processedDate { get; set; }
}
我使用下面的代码来反序列化json,但所有UserDocument属性都为null
var test = JsonConvert.DeserializeObject<List<UserDocument>>(jsonString);
为什么我将所有UserDocument属性都设为null,这里有什么问题?我没有收到任何错误。
您也可以建议将CouchBase queryresult转换为.net对象的好例子。
答案 0 :(得分:4)
好像你的json格式不正确。如果我说你的json就像
bd = "!git-bd"
然后创建一个类似
的模型 var percent = itemCost / totalCost * 100;
并将[
"document":
{
"createdDate":1476996267864,
"processedDate":1476996267864,
"taxYear":"2015",
"type":"user_document"
},
"document":
{
"createdDate":1476998303463,
"processedDate":0,
"taxYear":"2015",
"type":"user_document"
}
]
模型的public class Document
{
public UserDocument document {get;set;}
}
和UserDocument
属性更改为createdDate
,因为它类似于您的json
processedDate
然后反序列化
double
答案 1 :(得分:1)
像这样(使用Newtonsoft.Json.Linq):
var documents = JArray.Parse(json).Select(t => t["document"].ToObject<UserDocument>());