system.text.json with dynamic object names

Bernd Schmal 21 Reputation points
2021-12-20T08:21:59.243+00:00

I have a problem with dynamic object names. Can anyone please help me with deserializing this json into c# classes:

{
"WohnzimmerEG": {
"class": "org.openhab.core.items.ManagedItemProvider$PersistedItem",
"value": {
"groupNames": [
"EG"
],
"itemType": "Group",
"tags": [
"Room"
],
"label": "Wohnzimmer",
"category": "sofa"
}
},
"TreppenhausOG": {
"class": "org.openhab.core.items.ManagedItemProvider$PersistedItem",
"value": {
"groupNames": [],
"itemType": "Group",
"tags": [],
"label": "Treppenhaus",
"category": "door"
}
},
"Kinozimmer": {
"class": "org.openhab.core.items.ManagedItemProvider$PersistedItem",
"value": {
"groupNames": [
"OG"
],
"itemType": "Group",
"tags": [
"Location"
],
"label": "Kinozimmer",
"category": ""
}
}
}

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,277 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.5K Reputation points
    2021-12-20T12:39:12.22+00:00

    Check one of approaches:

    public class Data
    {
        public string @class { get; set; }
        public Value value { get; set; }
    }
    
    public class Value
    {
        public string[] groupNames { get; set; }
        public string itemType { get; set; }
        public string[] tags { get; set; }
        public string label { get; set; }
        public string category { get; set; }
    }
    
    . . .
    
    Dictionary<string, Data> dictionary = JsonSerializer.Deserialize<Dictionary<string, Data>>( json );
    

    where json is your string. See also other parameters of Deserialize.

    Sample usage:

    string category = dictionary["TreppenhausOG"].value.category;
    

    If required, use "Manage NuGet Packages" window to add a reference to System.Text.Json.


1 additional answer

Sort by: Most helpful
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2021-12-21T02:37:48.307+00:00

    @Bernd Schmal , Viorel-1's solution is good, I also make a code example for it, you could refer to the following code:

    using System;  
    using System.Collections.Generic;  
    using System.IO;  
    using System.Text.Json;  
      
    namespace ConsoleApp1  
    {  
        internal class Program  
        {  
            static void Main(string[] args)  
            {  
                string jsonstr = File.ReadAllText("D:\\1.json");  
                Root myDeserializedClass = JsonSerializer.Deserialize<Root>(jsonstr);  
      
                Console.WriteLine(myDeserializedClass.WohnzimmerEG.value.category);  
            }  
        }  
      
        public class Value  
        {  
            public List<string> groupNames { get; set; }  
            public string itemType { get; set; }  
            public List<string> tags { get; set; }  
            public string label { get; set; }  
            public string category { get; set; }  
        }  
      
        public class WohnzimmerEG  
        {  
            public string @class { get; set; }  
            public Value value { get; set; }  
        }  
      
        public class TreppenhausOG  
        {  
            public string @class { get; set; }  
            public Value value { get; set; }  
        }  
      
        public class Kinozimmer  
        {  
            public string @class { get; set; }  
            public Value value { get; set; }  
        }  
      
        public class Root  
        {  
            public WohnzimmerEG WohnzimmerEG { get; set; }  
            public TreppenhausOG TreppenhausOG { get; set; }  
            public Kinozimmer Kinozimmer { get; set; }  
        }  
    }  
       
    

    You could get the WohnzimmerEG's category like the following:

    Console.WriteLine(myDeserializedClass.WohnzimmerEG.value.category);

    Result:

    159070-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments