Dynamically parse nested JSon data to C# class

Is there a way to make to rewrite this dynamically?  There are over 500 "public inner" / "[JsonProperty(PropertyName =" in this Json data.

class Program
    {
        static string json = "{\"OBITS/BTS\":{\"timestamp\":1518205774653,\"asks\":[],\"bids\":[]},\"MSCN/RUR\":{\"timestamp\":1518205774654,\"asks\":[],\"bids\":[]}}";
        static void Main(string[] args)
        {
            var data = JsonConvert.DeserializeObject<Outer>(json);
            Console.WriteLine("OBITS/BTS - TimeStamp: {0}; Asks: {1}; Bids: {2}", data.ObitsBts.Timestamp, data.ObitsBts.Asks.Count, data.ObitsBts.Bids.Count);
            Console.WriteLine("MSCN/RUR - TimeStamp: {0}; Asks: {1}; Bids: {2}", data.MscnRur.Timestamp, data.MscnRur.Asks.Count, data.MscnRur.Bids.Count);
            Console.ReadLine();
        }
    }

    class Outer
    {
        [JsonProperty(PropertyName = "OBITS/BTS")]
        public Inner ObitsBts { get; set; }
        [JsonProperty(PropertyName = "MSCN/RUR")]
        public Inner MscnRur { get; set; }
    }

    class Inner
    {
        public BigInteger Timestamp { get; set; } 
        public List<int> Asks { get; set; }
        public List<int> Bids { get; set; }
    }

Open in new window

CAMPzxzxDeathzxzxAsked:
Who is Participating?
 
Nitin SontakkeConnect With a Mentor DeveloperCommented:
Is it safe to assume that your problem is solved?
0
 
Nitin SontakkeDeveloperCommented:
No idea about your comfort level / expertise in C#.

Did you manage to have a look into dynamic keyword in C#? If not, please do.

It may take a while for me to write a sample code, so posting this just to give you a head-start.

Do let me know if further help is required. Also if you could post complex enough json to try with, please.
0
 
CAMPzxzxDeathzxzxAuthor Commented:
I'm comfortable with C# and SQL but not JSON.  I know that this data is not valid...
here is a snippet:

{"OBITS/BTS":{"timestamp":1518205774653,"asks":[],"bids":[]},"MSCN/RUR":{"timestamp":1518205774654,"asks":[],"bids":[]}

Open in new window


 so now I'm attempting this...


json = json.Substring(json.IndexOf("[") + 1);
json = json.Substring(0, json.LastIndexOf("]"));
var rootObjects = JsonConvert.DeserializeObject<RootObject>(json);

Open in new window

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
CAMPzxzxDeathzxzxAuthor Commented:
 HttpResponseMessage message = httpClient.GetAsync(uri).Result;

var inter = message.Content.ReadAsStringAsync();

string obj = JsonConvert.DeserializeObject(inter.Result).ToString();

Open in new window


Get's me this..

JSON visualizer
The original question shows a class structure that works..

class Outer
    {
        [JsonProperty(PropertyName = "OBITS/BTS")]
        public Inner ObitsBts { get; set; }
        [JsonProperty(PropertyName = "MSCN/RUR")]
        public Inner MscnRur { get; set; }
    }

    class Inner
    {
        public BigInteger Timestamp { get; set; } 
        public List<int> Asks { get; set; }
        public List<int> Bids { get; set; }
    }

Open in new window



But I'm looking to parse these dynamically.
0
 
CAMPzxzxDeathzxzxAuthor Commented:
I got it into a data form I can use!

HttpResponseMessage message = httpClient.GetAsync(uri).Result;

                    var inter = message.Content.ReadAsStringAsync();

                    string obj = JsonConvert.DeserializeObject(inter.Result).ToString();

                    var x = (JsonConvert.DeserializeObject<IDictionary<string, object>>(obj));

Open in new window

0
 
CAMPzxzxDeathzxzxAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.