问题描述
我有一个JSON模型。但是我不确定要对结构进行建模。 所以ı通过https://json2csharp.com/创建了模型。
我想解析的Json structre:
[
{
"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; }
}
并解析代码:
Root result = JsonConvert.DeserializeObject<Root>(json);
但是我得到一个错误:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Tesr.Parser.Test.Root' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection,IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '',line 1,position 1.
堆栈跟踪:
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(JsonReader reader,Type objectType,JsonContract contract)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader,JsonContract contract,JsonProperty member,Object existingValue,String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader,JsonContainerContract containerContract,JsonProperty containerMember,Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader,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)
我查看了所有字段,但ı无法提出解决方案。有人有什么想法吗?
解决方法
尝试一下。您的JSON根元素是一个数组。
List<Root> result = JsonConvert.DeserializeObject<List<Root>>(json);
,
在您的情况下,Root类似乎无法使用。为了工作,您需要将MyArray属性添加到原始json。
但是,我认为您最好的选择是使用:
List<MyArray> result = JsonConvert.DeserializeObject<List<MyArray>>(json);