Json newtonsoft: Deserialize An object containing a list of strings

I have the following problem with this json:

{
"EVTS": {
"EVT": [
  { "ID": "123456",
    "KEY1" : "somekey",
    "CATEG": [
      "cat1",
      "cat2",
      "cat3"
    ]
  }
  ]}
  }

and this C # class:

public class myClass{
    public string ID { get; set; }
    public string KEY1 { get; set; } 

    public list<string> CATEG { get; set; } 
}

public class ESObject1
{

    [JsonProperty("EVT")]
    public List<myClass> EVT { get; set; }
}

public class ESObject0
{

    [JsonProperty("EVTS")]
    public ESObject1 EVTS { get; set; }
}

}

here i call the deserializer:

ESObject0 globalobject = JsonConvert.DeserializeObject<ESObject0>(json);

But this last code does not work, I selected this exception: System.ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.List1 [System.String] .`

Instead, list<string>I used string []and only stringnothing works.

how can i properly deserialize this object.

Thank.

+4
source share
2 answers

There does not seem to be any obvious problem with the hir code, as this working example illustrates:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;

public class myClass
{
    public string ID { get; set; }
    public string KEY1 { get; set; } 
    public List<string> CATEG { get; set; } 
}

public class ESObject1
{
    [JsonProperty("EVT")]
    public List<myClass> EVT { get; set; }
}

public class ESObject0
{
    [JsonProperty("EVTS")]
    public ESObject1 EVTS { get; set; }
}


class Program
{
    static void Main()
    {
        string json = 
        @"{
            ""EVTS"": {
                ""EVT"": [
                    {
                        ""ID"": ""123456"",
                        ""KEY1"": ""somekey"",
                        ""CATEG"": [
                            ""cat1"",
                            ""cat2"",
                            ""cat3""
                        ]
                    }
                ]
            }
        }";

        ESObject0 globalobject = JsonConvert.DeserializeObject<ESObject0>(json);
        foreach (string item in globalobject.EVTS.EVT[0].CATEG)
        {
            Console.WriteLine(item);
        }
    }
}

, json , , . , , , JSON, , KEY1.


UPDATE:

, JSON ( http://donnees.ville.quebec.qc.ca/Handler.ashx?id=69&f=JSON), , CATEG , :

""CATEG"": ""Conférence""

, . , , , JObject , .

:

var obj = JObject.Parse(json);
var events = (JArray)obj["EVTS"]["EVT"];
foreach (JObject evt in events)
{
    var categories = evt["CATEG"];
    if (categories is JArray)
    {
        // you've got a list of strings so you can loop through them
        string[] cats = ((JArray)categories)
            .Select(x => x.Value<string>())
            .ToArray();
    }
    else
    {
        // you've got a simple string
        string cat = categories.Value<string>();
    }
}
+13

. - json , , (http://json2csharp.com/).

nullable (, int? int), .

+3

Source: https://habr.com/ru/post/1532987/


All Articles