我想使用Json.net
将json对象反序列化为SQL DBJson文件:
{
"requirements": {
"requirement": [
{
"Id": "req1",
"desc": "description of requirement1"
},
{
"Id": "req2",
"desc": "description of requirement2",
},
{
"desc": "description of requirement3",
"Id": "req3"
}
]
}
}
这样'Id'属性的值应该放在Requirement表的ID列中,'desc'属性的值应该放在Requirement table的DESC列中
ID DESC
req1 description of requirement1
req2 description of requirement2
req3 description of requirement3
答案 0 :(得分:2)
您可以使用json2sharp工具生成课程,并像这样使用反序列化对象。
public class Requirement
{
public string Id { get; set; }
public string desc { get; set; }
}
public class Requirements
{
public List<Requirement> requirement { get; set; }
}
public class RootObject
{
public Requirements requirements { get; set; }
}
var x =JsonConvert.DeserializeObject<RootObject>(json);
并使用OrderByDescending
x.requirements.requirement.OrderByDescending(d => d.Id);
答案 1 :(得分:1)
string json = "{\"requirements\": {\"requirement\": [{\"Id\": \"req1\",\"desc\": \"description of requirement1\"},{\"Id\": \"req2\",\"desc\":\"description of requirement2\",},{\"desc\": \"description of requirement3\",\"Id\": \"req3\"}]}}";
//Deserialize using Json.Net
var objectRequirement = JsonConvert.DeserializeObject<RootObject>(json);
//Add it to your database
foreach (var item in objectRequirement.requirements.requirement)
{
db.Requirement.Add(new Requirement { ID = item.Id, DESC = item.desc });
}
db.SaveChanges();
<小时/> 您可以使用http://json2csharp.com/创建班级:
public class Requirement
{
public string Id { get; set; }
public string desc { get; set; }
}
public class Requirements
{
public List<Requirement> requirement { get; set; }
}
public class RootObject
{
public Requirements requirements { get; set; }
}