我需要使用C#解析JSON数据,这是数组的集合。
execution()
我可以解析简单的JSON字符串但是当它出现时,我应该如何继续?
答案 0 :(得分:0)
您可以使用Json2csharp为json生成类。然后使用nuget中的Json.Net:
void Main()
{
var json = @"{ItemRelations: [
{
rel: ""System.Links.H-Forward"",
source: {id: 123456,url: ""https://somename.domain.com/DefaultCollection/_apis/wit/Items/123456""},
target: {id: 231856,url: ""https://somename.domain.com/DefaultCollection/_apis/wit/Items/231856""}
}
]}";
var parsed = JsonConvert.DeserializeObject<RootObject>(json);
//Linqpad
//parsed.Dump();
}
public class Source
{
public int id { get; set; }
public string url { get; set; }
}
public class Target
{
public int id { get; set; }
public string url { get; set; }
}
public class ItemRelation
{
public string rel { get; set; }
public Source source { get; set; }
public Target target { get; set; }
}
public class RootObject
{
public List<ItemRelation> ItemRelations { get; set; }
}