I am trying to parse a JSON feed using LINQ and can't quite wrap a list of objects/values into a reasonable class.
My JSON looks like:
{
"Project":[
{
"ID":"XY1212",
"Name":"Some Name",
"Description":"U.S. No 2 Diesel Retail Prices",
"Manager":"Nora Sims",
"Dept":"HR",
"Updated":"2014-07-22",
"Statistics":[
[
"20140722",
32.22
],
[
"20140721",
55
],
[
"20140720",
343
],
[
"20140519",
43
],
[
"20140421",
3.971
],
[
"20140211",
40.2
],
[
"20140210",
17
],
[
"20140209",
16
]
]
}
]
}
From the above JSON, I have the following class structure:
public class Project
{
public string ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Manager { get; set; }
public string Dept { get; set; }
public string Updated { get; set; }
public List<List<object>> Statistics { get; set; }
}
public class RootObject
{
public List<Project> Project { get; set; }
}
public class Statistic
{
public Datetime ProjectDate { get; set; }
public Decimal Sale { get; set; }
}
I am trying to parse the feed and just want the "Statistics" data, but not sure how to get all of the values into the collection of "Statistics":
HttpClient() http = new HttpClient();
var json = await http.GetStringAsync(uri);
JObject jo = JObject.Parse(json);
var jList = from values in jo["Project"].Children()["Statistics"]
select values;
When I inspect jList via the following loop:
foreach (var stat in jList)
{
Console.WriteLine(stat);
}
I can "see" all of the values, but it only loops once, i.e. jList is only one big [0] with a "value" of all of the [x, y], [x1, y1], ..., i.e. looks like an array of one dimension with many 2D arrays inside it.
I want to loop through all of the "arrays", I believe that's what they are, within the [0] I see in Visual Studio while debugging.
Any advice appreciated.
You can solve this easily by making a custom JsonConverter for your Statistic class. It will deal with the unconventional JSON and allow you to define your Project class the way you'd really like to have it.
Here is the code for the converter:
class StatisticConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(Statistic));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JArray array = JArray.Load(reader);
return new Statistic
{
ProjectDate = DateTime.ParseExact(array[0].ToString(), "yyyyMMdd",
System.Globalization.CultureInfo.InvariantCulture),
Sale = array[1].ToObject<decimal>()
};
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
To use the converter, we just need to make a couple of minor changes to your classes. First, change the Statistics property to be a List<Statistic> instead of a List<List<object>>. (Don't worry that it doesn't match the JSON-- that's what the converter is for.)
public class Project
{
public string ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Manager { get; set; }
public string Dept { get; set; }
public string Updated { get; set; }
public List<Statistic> Statistics { get; set; }
}
Next decorate your Statistic class with a [JsonConverter] attribute to tie it to the custom converter:
[JsonConverter(typeof(StatisticConverter))]
public class Statistic
{
public DateTime ProjectDate { get; set; }
public Decimal Sale { get; set; }
}
That's it! Now you can deserialize as normal, and you'll get a list of statistics the way you want.
RootObject root = JsonConvert.DeserializeObject<RootObject>(json);
Working demo here.
The problem isn't your code so much as it is the JSON string that is coming back from the HttpClient.
Because it has parts that don't have a key, it is hard to sort through, also each child of statistic is a child of that child (If my eyes don't deceive me).
However, using the code below, I am able to read the Statistics values singularly
JObject jo = JObject.Parse(json);
var jList = from values in jo["Project"]
select values;
foreach (var j in jList)
{
var l = j["Statistics"].Children();
foreach (var m in l.Children())
{
string a = m.ToString();
}
}
On the first loop, m has the Date, On the second loop it has the sale. because there is no key for these, I don't know any other way to reference them.
A bit less code but similar result
JObject jo = JObject.Parse(json);
for (int i = 0; i < jo.Count; i++)
{
var jList = from values in jo["Project"][i]["Statistics"]
select values;
foreach (var stat in jList)
{
//here stat looks like {["20140722", 32.22]}
}
}
Related
I have this Json:
{
"UpdatePack":"updatePacks\/1585654836.pack",
"Updates":[
{
"Name":"MsgBoxEx",
"version":"1.5.14.88",
"ChangeLog":"BugFix: Form didn't resize correct.",
"Hash":"5FB23ED83693A6D3147A0485CD13288315F77D3D37AAC0697E70B8F8C9AA0BB8"
},
{
"Name":"Utilities",
"version":"2.5.1.58",
"ChangeLog":"StringManagement updated.",
"Hash":"05E6B3F521225C604662916F50A701E9783E13776DE4FCA27BE4B69705491AC5"
}
]
}
I have created 2 classes to be used to Deserialize it.
class UpdatesList
{
public string Name { get; set; }
public string Version { get; set; }
public string ChangeLog { get; set; }
public string Hash { get; set; }
}
class JsonObjectHolder
{
public string UpdatePack { get; set; }
//public Dictionary<int, MyData> { get; set; }
public Dictionary<int, UpdatesList> Updates { get; set; }
}
But when I try to access the dictionary, I keep getting Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at " Console.WriteLine(jsonTest.Dict.Count);"
Am I Deserializing it wrong, or do I need to do some thing else to access the result of the dictionary?
I'm new to both C# and Json.
I hope that some one could point me in the right direction on how to handle this.
I'm using Visual Studio 2019 latest update, and .net 4.8.
Regards
/LR
You code doesn't work because 0 and 1 tokens just a properties, not the array items (you don't have square brackets [] around them). You can parse these values to desired structure manually using JObject
var json = JObject.Parse(your_json_string);
var dict = new Dictionary<int, UpdatesList>();
foreach (var item in json.Properties())
{
if (item.Value.Type == JTokenType.Object)
{
var index = int.Parse(item.Name);
var updateList = item.Value.ToObject<UpdatesList>();
dict.Add(index, updateList);
}
}
var holder = new JsonObjectHolder
{
UpdatePack = json["Updates"]?.Value<string>(),
Dict = dict
};
Update: According to OP changes made to JSON it might be deserialized even more simply
var list = json["Updates"]?.ToObject<List<UpdatesList>>();
var holder = new JsonObjectHolder
{
UpdatePack = json["UpdatePack"]?.Value<string>(),
Dict = list.Select((updatesList, index) => new { updatesList, index })
.ToDictionary(x => x.index, x => x.updatesList)
};
The main point here is that Updates is an array of items, not the key-value collection. It can be transformed into Dictionary<int, UpdatesList> using ToDictionary method from System.Linq (or just use List<UpdatesList> as is)
The exception you're getting essentially means the value is being accessed before the object is initialized.
A better, simpler and cleaner way to doing it is using NewtonSoft. (you can easily get it as a Nuget package)
example:
public class Account
{
public string Email { get; set; }
public bool Active { get; set; }
public DateTime CreatedDate { get; set; }
public IList<string> Roles { get; set; }
}
and then usage:
string json = #"{
'Email': 'james#example.com',
'Active': true,
'CreatedDate': '2013-01-20T00:00:00Z',
'Roles': [
'User',
'Admin'
]
}";
Account account = JsonConvert.DeserializeObject<Account>(json);
Console.WriteLine(account.Email);
Source: https://www.newtonsoft.com/json/help/html/DeserializeObject.htm
I don't see why you need Dictionary<int, UpdatesList> Updates, when you can easily just use List<Update> Updates, since your updates are in a JSON array.
I would model your classes like this:
public class Update
{
public string Name { get; set; }
public string Version { get; set; }
public string ChangeLog { get; set; }
public string Hash { get; set; }
}
public class RootObject
{
public string UpdatePack { get; set; }
public List<Update> Updates { get; set; }
}
You can then deserialize with:
JsonConvert.DeserializeObject<RootObject>(json);
Try it out on dotnetfiddle.net
Note: To convert JSON to C# classes, you can go to Edit -> Paste Special -> Paste JSON as Classes inside Visual Studio. Make sure you have copied the JSON to your clipboard before using it. You will get classes similar to above.
your data and the class is not compatible. if you change the string like this it would work.
change "Updates" to "UpdatePack" and add "Dict" around the dictionary items.
{
"UpdatePack":"updates\/4D1D7964D5B88E5867324F575B77D2FA.zip",
"Dict":{
"0":{
"Name":"MsgBoxEx",
"Version":"1.0.123.58",
"ChangeLog":"Bugfix:Form didn't resize correct",
"hash":"AA94556C0D2C8C73DD217974D252AF3311A5BF52819B06D179D17672F21049A6"
},
"1":{
"Name":"Utilities",
"Version":"1.5.321.87",
"ChangeLog":"StringManagement updated",
"hash":"2F561B02A49376E3679ACD5975E3790ABDFF09ECBADFA1E1858C7BA26E3FFCEF"
}
}
}
Does Newtonsoft.JSON library have a simple way I can automatically deserialize JSON into 2 different Models/classes?
For example I get the JSON:
[{
"guardian_id": "1453",
"guardian_name": "Foo Bar",
"patient_id": "938",
"patient_name": "Foo Bar",
}]
And I need de-serialize this to the following models:
class Guardian {
[JsonProperty(PropertyName = "guardian_id")]
public int ID { get; set; }
[JsonProperty(PropertyName = "guardian_name")]
public int Name { get; set; }
}
class Patient {
[JsonProperty(PropertyName = "patient_id")]
public int ID { get; set; }
[JsonProperty(PropertyName = "patient_name")]
public int Name { get; set; }
}
Is there a simple way to deserialize this JSON into 2 Models without having to iterate over the JSON? Maybe JSON property ids will just work?
Pair<Guardian, Patient> pair = JsonConvert.DeserializeObject(response.Content);
First off, your models are slightly incorrect. The name properties need to be strings, instead of integers:
class Guardian
{
[JsonProperty(PropertyName = "guardian_id")]
public int ID { get; set; }
[JsonProperty(PropertyName = "guardian_name")]
public string Name { get; set; } // <-- This
}
class Patient
{
[JsonProperty(PropertyName = "patient_id")]
public int ID { get; set; }
[JsonProperty(PropertyName = "patient_name")]
public string Name { get; set; } // <-- This
}
Once you've corrected that, you can deserialize the JSON string into two lists of different types. In your case, List<Guardian> and List<Patient> respectively:
string json = #"[{'guardian_id':'1453','guardian_name':'Foo Bar','patient_id':'938','patient_name':'Foo Bar'}]";
var guardians = JsonConvert.DeserializeObject<List<Guardian>>(json);
var patients = JsonConvert.DeserializeObject<List<Patient>>(json);
It you want to do it with 1 call, you need to create a class that matches the JSON. That class can then return Guardian and Patient objects as needed. Also you'll need to use an array or list for the return type because the source JSON is an array.
The class to create:
public class Pair
{
public Pair()
{
Guardian = new Guardian();
Patient = new Patient();
}
[JsonIgnore]
public Guardian Guardian { get; set; }
[JsonIgnore]
public Patient Patient { get; set; }
[JsonProperty(PropertyName = "guardian_id")]
public int GuardianID
{
get { return Guardian.ID; }
set { Guardian.ID = value; }
}
[JsonProperty(PropertyName = "guardian_name")]
public string GuardianName
{
get { return Guardian.Name; }
set { Guardian.Name = value; }
}
[JsonProperty(PropertyName = "patient_id")]
public int PatientID
{
get { return Patient.ID; }
set { Patient.ID = value; }
}
[JsonProperty(PropertyName = "patient_name")]
public string PatientName
{
get { return Patient.Name; }
set { Patient.Name = value; }
}
}
And how to use it:
var pairs = JsonConvert.DeserializeObject<Pair[]>(response.Content);
if (pairs.Any())
{
var pair = pairs[0];
Console.WriteLine(pair.Guardian.Name);
Console.WriteLine(pair.Patient.Name);
}
Not in one call, and it seems the data is an array, so you need a little more work.
Zip is the key method here to join the two separate object lists:
Guardian[] guardians = JsonConvert.DeserializeObject<Guardian[]>(response.Content);
Patient[] patients = JsonConvert.DeserializeObject<Patient[]>(response.Content);
var combined = guardians.Zip(patients, (g, p) => Tuple.Create(g, p)).ToList();
It would be far more easier to just read the JSON at once, it a single object.
It can't be done with 1 call with the types that you show. You can try using the generic <T> approach for each type, also you'll need to use arrays or lists for the return type because the source JSON is an array:
var guardians = JsonConvert.DeserializeObject<Guardian[]>(response.Content);
var patients = JsonConvert.DeserializeObject<Patient[]>(response.Content);
And then combine the two if you need them to be paired. E.g. if you are sure that you always have just one of each:
var pair = new Pair(guardians[0], patients[0]);
You could make a type to house the two subobjects:
[JsonConverter(typeof(GuardianPatientConverter))]
class GuardianPatient
{
public Guardian Guardian { get; set; }
public Patient Patient { get; set; }
}
And then create a JSON converter to handle the JSON:
class GuardianPatientConverter : JsonConverter
{
public override bool CanRead
{
get { return true; }
}
public override bool CanWrite
{
get { return false; }
}
public override bool CanConvert(Type objectType)
{
return typeof(GuardianPatient) == objectType;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
return null;
}
var jObject = JObject.Load(reader);
var guardian = new Guardian();
var patient = new Patient();
serializer.Populate(jObject.CreateReader(), guardian);
serializer.Populate(jObject.CreateReader(), patient);
return new GuardianPatient()
{
Guardian = guardian,
Patient = patient
};
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
And then you can use it like so:
var json = "[{\"guardian_id\":\"1453\",\"guardian_name\":\"Foo Bar\",\"patient_id\":\"938\",\"patient_name\":\"Foo Bar\",}]";
var objects = JsonConvert.DeserializeObject<IEnumerable<GuardianPatient>>(json);
and if you want it as an array of pairs:
var objects = JsonConvert.DeserializeObject<IEnumerable<GuardianPatient>>(json)
.Select(o => new Pair(o.Guardian, o.Patient))
.ToArray();
This won't make it any faster, but I suspect you're looking for an easier way to work with the JSON.
One another approach would be creating class that matches JSON format, i.e. class with four properties with corresponding names. Then, deserialize JSON into that class and then use it in your code (set properties of objects with values from JSON, pass deserialized object to constructor of another class).
In your models, The name properties need to be strings, instead of integers. After correcting it.
You can use Tuple class
string json = #"[{'guardian_id':'1453','guardian_name':'Foo Bar','patient_id':'938','patient_name':'Foo Bar'}]";
var combination = new Tuple<List<Guardian>, List<Patient>>(JsonConvert.DeserializeObject<List<Guardian>>(json), JsonConvert.DeserializeObject<List<Patient>>(json));
static void Main(string[] args)
{
string json = JsonConvert.SerializeObject(new[]
{
new
{
guardian_id = "1453",
guardian_name = "Foo Bar",
patient_id = "938",
patient_name = "Bar Foo",
}
});
Guardian[] guardians = JsonConvert.DeserializeObject<Guardian[]>(json);
Patient[] patients = JsonConvert.DeserializeObject<Patient[]>(json);
}
Since both your objects are the same, wouldn't it make more sense to just have an ID/Name structure of a single base class? If you need to send all the data at the same time, you can restructure your data and use a data transfer object pattern. The JSON object would become
[{
"guardian": {
"id": "1453",
"name": "Foo Bar"
},
"patient": {
"id" : "938",
"name": "Foo Bar"
}
}]
And your corresponding data objects would be:
public class Record {
public int id { get; set; } // or string. I'm not sure which would be more appropriate
public string name { get; set;}
}
and
public class RecordDto {
public Record guardian { get; set; }
public Record patient { get; set; }
}
And your API would receive a
List<RecordDto>
parameter (since you are passing an array of objects).
Using JSON.NET I am stuck on a issue with MasterData with regards to GET and POST operations.
Using the example of a movie, the JSON I could get on a GET operation before deserializing is the following:
{
“movie”: {
“name”:”bad boys”,
”genre”: {
“id”:"1",
”name”:”thriller”
}
}
}
However, on a POST operation I would like to construct a serialized string like the following:
{
“movie”: {
“name”:”bad boys”,
”genre”:"1"
}
}
As you can see, with a GET operation I need to deserialize the complete object, while on serialize for POST I need to add the Id only, but with the same JsonPropertyName.
What is the cleanest way to obtain this situation? I've been trying to work on the IContractResolver and IReferenceResolver, but those both didn't get me there yet. Also have been trying to work with propertynames as the following example:
[JsonProperty(PropertyName = "genre")]
public Genre Genre { get; set; }
[JsonProperty(PropertyName = "genre")]
public string GenreId { get { return Genre != null ? Genre.Id : 0; } }
I've also tried using the ShouldSerialize[MemberName] and ShouldDeserialize[MemberName] situations, but those gave an exception saying I cannot use two properties with the same name, which is logical in a way.
For now, my only idea left is to use a DTO for serialization, but I would rather prefer something more clean than that solution. So open for suggestions!
You can write a Custom converter.
public class GenreConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Genre);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return JObject.Load(reader).ToObject<Genre>();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var genre = value as Genre;
writer.WriteValue(genre.id);
}
}
All you need now is using JsonConverter atttribute
public class Movie
{
public string Name { get; set; }
[JsonConverter(typeof(GenreConverter))]
public Genre Genre { get; set; }
}
Use custom converter for this. You want to check type of genre attribute and read it as object or int.
In my opinion, using a DTO is the cleanest solution. Since your models are different for GET/POST, you ideally require two separate models. I've abused the NullValueHandling.Ignore serializer setting to mean you can use the same model for both situations, providing you don't mind always having the Id property on Genre:
class MovieJsonWrapper
{
[JsonProperty("movie")]
public Movie Movie { get; set; }
}
class Movie
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("genre")]
public Genre Genre { get; set; }
}
class Genre
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
We can then use this with:
var movie = new Movie
{
Name = "bad boys",
Genre = new Genre
{
Id = "1"
}
};
var movieJsonWrapper = new MovieJsonWrapper { Movie = movie };
var jsonSerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
var json = JsonConvert.SerializeObject(movieJsonWrapper, jsonSerializerSettings);
Which would produce the following:
{
"movie": {
"name": "bad boys",
"genre": {
"id": "1"
}
}
}
Similarly, we can then deserialize the GET response with:
var result = JsonConvert.DeserializeObject<MovieJsonWrapper>(raw);
If you really do need to ditch the Id property on the Genre when POSTing, then you would require additional Models. This solution reduces the amount of boilerplate.
I'm new to Json and trying to understand how I can parse it using Json.Net. I've tried to create objects for my json input, but I'm stuck. I'm not quite sure how to parse the input so I can iterate through it and output the season numbers and episode name.
Anyone who can point me in the right direction?
Json:
{
"data":{
"1921":{
"1":{
"airdate":"1921-03-20",
"name":"Cleaning Up!!?",
"quality":"N/A",
"status":"Wanted"
},
"2":{
"airdate":"1921-03-20",
"name":"Kansas City Girls Are Rolling Their Own Now",
"quality":"N/A",
"status":"Wanted"
},
"3":{
"airdate":"1921-03-20",
"name":"Did You Ever Take a Ride Over Kansas City Street 'in a Fliver'",
"quality":"N/A",
"status":"Wanted"
},
"4":{
"airdate":"1921-03-20",
"name":"Kansas City's Spring Clean-Up",
"quality":"N/A",
"status":"Wanted"
}
},
"1923":{
"1":{
"airdate":"2013-05-16",
"name":"Alice's Wonderland - aka - Alice in Slumberland",
"quality":"Unknown",
"status":"Downloaded"
}
}
},
"message":"",
"result":"success"
}
Code:
static void Main(string[] args)
{
RootObject data = JsonConvert.DeserializeObject<RootObject>(System.IO.File.ReadAllText(#"C:\Users\Benjamin\Desktop\json\input.txt"));
foreach (var e in data)
{
// Being able to output Season and Episode name like:
// 1921 - Cleaning Up!!?
}
}
public class RootObject
{
public Dictionary<int, Season> data { get; set; }
public string message { get; set; }
public string result { get; set; }
}
public class Season
{
public Dictionary<string, Episode> number { get; set; }
}
public class Episode
{
public string airdate { get; set; }
public string name { get; set; }
public string quality { get; set; }
public string status { get; set; }
}
With a change to your RootObject:
public class RootObject
{
public Dictionary<int, Dictionary<string, Episode>> data { get; set; }
public string message { get; set; }
public string result { get; set; }
}
You can then do this:
RootObject root = JsonConvert.DeserializeObject<RootObject>(data);
foreach (var s in root.data)
{
foreach (var e in s.Value)
{
Console.WriteLine(string.Format("{0} - {1}", s.Key, e.Value.name));
// access whatever properties you want here...
}
}
Note, we have eliminated the Season object because JSON.net won't be able to map to the property number because that's not a property in your original JSON (you can make it work, but it would require some fiddling around with custom serialization).
If you really want a LINQ solution, then something like:
root.data.ToList().ForEach(s =>
{
s.Value.ToList().ForEach(e =>
{
Console.WriteLine(string.Format("{0} - {1}", s.Key, e.Value.name));
});
});
Achieves the same thing, but requires copying your dictionary into a list which might be a problem if your dictionary is very large.
I have the following issue 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 doesnt work, i throws this exception : System.ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.List1[System.String].`
Instead of list<string> i used string [] and only string nothing seems to work.
how can i deserialize this object correctly please.
Thank you.
There doesn't seem to be any apparent problem wit hyour 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);
}
}
}
Maybe you just fed a wrong json value to the deserializer which doesn't look like as the one shown in your question. By the way, the one shown i nyour question is invalid JSON as you are missing a , after KEY1 property declaration.
UPDATE:
Now that you have shown your real JSON (coming from http://donnees.ville.quebec.qc.ca/Handler.ashx?id=69&f=JSON) it appears that there's a row where CATEG is not an array of strings but a simple string:
""CATEG"": ""Conférence""
Now that's a pretty bad design because they are mixing arrays and simple properties. I am afraid that in order to deal with this situation you will need to use JObjects and extract the information you need by testing the actual underlying type.
For example:
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>();
}
}
I have done this many times with many many headaches. My advice is take the json output and use a tool similar to this to write your class for you (http://json2csharp.com/).
Then go over any nullable variables and add nullable type (ex. using int? for int) where needed.