How can I serialize/deserialize a kind of json mixing object and list of object at the same level in c#?

ebelair

I'm using newtonsoft json, and I need to serialize/deserialize this kind of json:

[
  {
    "value": "value1",
    "field": "field1"
  },
  {
    "value": "value2",
    "field": "field2"
  },
  {
    "value": "value3",
    "field": "field3"
  },
  [
    {
      "value": "value4",
      "field": "field4"
    },
    {
      "value": "value4",
      "field": "field5"
    }
  ]
]

As you can see, I can have an object or a list of this object at the same level. I've created a class for the object but I don't know how to make the serialization process this kind of json, or if it's possible to do.

EDIT: I don't have the choice to process that json as it comes to me. So please, if you think I like this, you're wrong.

Serge

you can create a custom json converter

Result result = JsonConvert.DeserializeObject<Result>(json, new ArrayJsonConverter())

public class ArrayJsonConverter : JsonConverter<Result>
{
    public override Result ReadJson(JsonReader reader, Type objectType, Result existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        JArray jArr = JArray.Load(reader);

        var result = new Result { Items = new List<Item>(), ListItems = new List<List<Item>>() };

        foreach (var item in jArr)
            if (item.Type == JTokenType.Array) result.ListItems.Add(item.ToObject<List<Item>>());
            else result.Items.Add(item.ToObject<Item>());

        return result;
    }

    public override void WriteJson(JsonWriter writer, Result value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

public class Result
{
    public List<Item> Items { get; set; }
    public List<List<Item>> ListItems { get; set; }
}

public class Item
{
    public string value { get; set; }
    public string field { get; set; }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

C# Unity JsonUtility, how can I turn this simple json into some kind of useful c# object?

How to add json object into another json object at the same level?

How to get level of each object in json where objects can have any number of children of same object type

How can create JSON Object multi level?

How can I read this kind of object using python?

how can i push json data to csharp list of object by javascript?

How can I add a new object to a list of objects in a .json file?

How can I have a list inside and object in JSON?

How I can get List object from JSON instead of a LinkedHashMap?

How can I define object type which is same as level 1 but differ rest levels in typescript?

How can i access an object in a list in c# query with ef?

How can I add a IDataReader object to List<IDataReader> in C#?

How can I call a method of an object in a list iterator in C++?

How to handle object and array at same level in json with Vue?

How can i convert an array in Json string to c# object?

How i can convert this JSON to Object in C#?

How can I return a C# Object instead of array JSON

How can I loop over a multi level JSON object and create a new Javascript object from one of it's key and values

how can i get an object inside another object "JSON" using json-c?

How can I Serialize an object as a method inside the same object in java?

How can I pass the same object to another method of another object?

Why can't I spread in an array inside an object that has another object at the same level being filtered

How can i convert a plain JSON object to nested JSON Object

JAVASCRIPT: how can I remove a json object in json object?

How can I return an object with a list inside of it?

How Can I get a List object by HttpClientResponse?

How can i get object from List

how can I read list of object of array

How can I filter the object in the List with Mongoose?