简体   繁体   中英

parse Dynamic c# class from a json

I want to build a dynamic class with a given JSON. Atm i parse the json with

dynamic customConfig = JsonConvert.DeserializeObject(configJson); 

and its working fine with other json then the given BUT my problem is that the names of the properties (here valueOne and valueTwo are "dynamic", i get always others)

i Know if i know the names i can get eg the description by customConfig.config.valueOne.description But what can i do to get eg the description by dont have the name of valueOne?

configJson=
"config": {
        "valueOne":{
                "description": "My first example value.",
                "defaultValue": "Example 1",
                                "isRequired":false
               },
        "valueTwo":{
                "description": "My second example value.",
                "defaultValue": "Example 2",
                                "isRequired":false
                   },

         },

What i tried was to get it in a loop but i dont get it to another class.

            foreach (var param in customConfig.config)
            {
                foreach (var item in param)
                {
                  Config.config.description[i] = item.description;
                }
             i++;
            }

item.description gets the right description but why i cant save it in the other class (which is also dynamic)?

You might have some other underlying logic issues with your loop and what you're trying to accomplish there, but to answer your specific question, you might need to initialize your "config" and "data" members of your CustomConfigModel class. For example...

public class CustomConfigModel
{ 
    public CustomConfigModel()
    {       
        this.data = new ExpandoObject();
        this.config = new ExpandoObject();
    }

    public dynamic data { get; set; } 
    public dynamic config { get; set; }
}

I was able to access the description you want, I think but it is in this form (my output) :

Token key: 'config.valueOne' ->>>> 'description' : 'My first example value.'
Token key: 'config.valueTwo' ->>>> 'description' : 'My first example value.'

If you know main item name , "config", before hand , you can parse and get rid of it so that you'd have values : "valueOne" or "valueTwo". As you can see from my example code, you can get description values by iterating. You can develop further functionality from this example. Please let me know if this works for you.

Here is the example code:

class Program
{
    static void Main(string[] args)
    {
        string configJson = @"{
          'config': {
    'valueOne':{
            'description': 'My first example value.',
            'defaultValue': 'Example 1',
                            'isRequired':false
           },
    'valueTwo':{
            'description': 'My second example value.',
            'defaultValue': 'Example 2',
                            'isRequired':false
               },

     }
        }";

        JObject customConfig = (JObject)JsonConvert.DeserializeObject(configJson);
        var children = customConfig.Children().Children();
        List<JToken> subList = new List<JToken>();


        foreach (JToken token in children.Values())
        {
            string key = token.Path;
            subList.AddRange(token.Values());
            JToken subitem = subList.Find(q => q.Path.Contains("description"));
            string desc = ((JProperty)subitem).Value.ToString();
            Console.WriteLine("Token key: '" + key + "' ->>>> 'description' : '" + desc+"'");
        }

    }
}

At

Config.config.description[i] = item.description;

is description[i] null and i get an excception but why

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