简体   繁体   中英

How can I parse complex JSON Result

I have a JSON model. But ı didn't ensure about to model structure. So ı created the model via https://json2csharp.com/ .

Json structre which ı want parse:

   [
        {
            "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": []
                }
            ]
        }]

The output I got is as follows :

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; } 
    }

And parsing code :

 Root result = JsonConvert.DeserializeObject<Root>(json); 

But ı get a error:

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.

Stack Trace:

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)

I review all field but ı couldn't produce a solution. Does anyone have any ideas?

Try this. Your JSON root element is an array.

List<Root> result = JsonConvert.DeserializeObject<List<Root>>(json);

The Root class looks unusable in your case. In order to work, you would need to add MyArray property to the original json.

However, I think your best option is using:

List<MyArray> result = JsonConvert.DeserializeObject<List<MyArray>>(json);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM