C# 如何解析复杂的JSON结果
我有一个JSON模型。但ı并没有确保即将建立结构模型。 因此,ı通过创建了模型 需要解析的Json结构:C# 如何解析复杂的JSON结果,c#,json,.net,C#,Json,.net,我有一个JSON模型。但ı并没有确保即将建立结构模型。 因此,ı通过创建了模型 需要解析的Json结构: [ { "id": 1, "dataTestId": "vuln-software-config-1", "totalCpeCount": 1, "containers": [
[
{
"id": 1,
"dataTestId": "vuln-software-config-1",
"totalCpeCount": 1,
"containers": [
{
"operator": "OR",
"depth": 1,
"dataTestId": "vuln-software-operator-1-0",
"configType": "BASIC",
"cpeListType": "VULNERABLE",
"cpes": [
{
"depth": 2,
"vulnerable": true,
"previous": false,
"urlName": "cpe%3A2.3%3Aa%3Aapache%3Atomcat%3A*%3A*%3A*%3A*%3A*%3A*%3A*%3A*",
"cpe23Uri": "cpe:2.3:a:apache:tomcat:*:*:*:*:*:*:*:*",
"cpe22Uri": "cpe:/a:apache:tomcat",
"status": null,
"resultingCpes": null,
"dataTestId": "vuln-software-cpe-1-0-0",
"id": "4282632",
"matchCpes": [],
"rangeDescription": " versions from (including) 7.0.0 up to (including)7.0.84",
"rangeStartType": "including",
"rangeStartVersion": "7.0.0",
"rangeEndType": "including",
"rangeEndVersion": "7.0.84",
"rangeId": "241999",
"rangeCpes": []
}
],
"containers": []
}
]
}]
我得到的结果如下:
public class Cpe {
public int depth { get; set; }
public bool vulnerable { get; set; }
public bool previous { get; set; }
public string urlName { get; set; }
public string cpe23Uri { get; set; }
public string cpe22Uri { get; set; }
public object status { get; set; }
public object resultingCpes { get; set; }
public string dataTestId { get; set; }
public string id { get; set; }
public List<object> matchCpes { get; set; }
public string rangeDescription { get; set; }
public string rangeStartType { get; set; }
public string rangeStartVersion { get; set; }
public string rangeEndType { get; set; }
public string rangeEndVersion { get; set; }
public string rangeId { get; set; }
public List<object> rangeCpes { get; set; }
}
public class Container {
public string operator { get; set; }
public int depth { get; set; }
public string dataTestId { get; set; }
public string configType { get; set; }
public string cpeListType { get; set; }
public List<Cpe> cpes { get; set; }
public List<object> containers { get; set; }
}
public class MyArray {
public int id { get; set; }
public string dataTestId { get; set; }
public int totalCpeCount { get; set; }
public List<Container> containers { get; set; }
}
public class Root {
public List<MyArray> MyArray { get; set; }
}
我查看了所有字段,但ı无法生成解决方案。有人有什么想法吗?试试这个。JSON根元素是一个数组
List<Root> result = JsonConvert.DeserializeObject<List<Root>>(json);
List result=JsonConvert.DeserializeObject(json);
根类在您的情况下看起来不可用。为了工作,需要将MyArray属性添加到原始json
但是,我认为您最好的选择是使用:
List<MyArray> result = JsonConvert.DeserializeObject<List<MyArray>>(json);
List result=JsonConvert.DeserializeObject(json);
您已经发布了堆栈跟踪,但没有发布实际的错误消息或异常类型…很抱歉,我添加了相关的异常消息它似乎是您的一个属性期望对象,但在JSON字符串中它是一个对象数组,
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(JsonReader reader, Type objectType, JsonContract contract)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
List<Root> result = JsonConvert.DeserializeObject<List<Root>>(json);
List<MyArray> result = JsonConvert.DeserializeObject<List<MyArray>>(json);