Hi,

I'm trying to desirealise the json data from a external file where i want to read the key and fetch another dictionary value based on key please letme know if i can read dictionary inside dictionary , I have tried using DIctionary<string,object> where as am looking for Dictionary <string,Dictionary<string,string>> format to read the data
sample json data :

                  {
       "PSQL": [
                {
                  "isClient": "\"SELECT  RULE_T FROM TABLE_TEST WHERE USR_ID = 'XYZ' AND PROD_ID = 'V'\";"
                }
              ]
            }

Recommended Answers

Dictionary <string,Dictionary<string,string>> should work correctly.

In the case that it doesn't, you can try to create a wrapper class for it, like so:

public class DictionaryStringString : Dictionary<string, string> { }

And use in your model Dictionary<string, DictionaryStringString>

Try it yourself first. If it still doesn't work let me know, …

Jump to Post

Show an example of a more complete JSON file.

Jump to Post

All 6 Replies

Dictionary <string,Dictionary<string,string>> should work correctly.

In the case that it doesn't, you can try to create a wrapper class for it, like so:

public class DictionaryStringString : Dictionary<string, string> { }

And use in your model Dictionary<string, DictionaryStringString>

Try it yourself first. If it still doesn't work let me know, I'll try to get it working.

Are you using JSON.NET ?

Ok, here' an example:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace JsonDictionaryTest
{
    internal class Program
    {
        private static void Main()
        {
            TestNested();
        }

        private static void TestNested()
        {
            Nested nested = new Nested()
            {
                Items = new Dictionary<string, Dictionary<string, string>>()
                {
                    {
                        "PSQL", new Dictionary<string, string>()
                        {
                            { "isClient", "\"SELECT RULE_T FROM TABLE_TEST WHERE USR_ID = 'XYZ' AND PROD_ID = 'V'\";" }
                        }
                    }
                }
            };

            string json = JsonConvert.SerializeObject(nested, Formatting.Indented);

            Nested nestedCopy = JsonConvert.DeserializeObject<Nested>(json);
        }
    }

    public class Nested
    {
        public Dictionary<string, Dictionary<string, string>> Items { get; set; }
    }
}

In Perl this called a hash of a hash and is accessed via pointing down and down to data value.

It is used in accessing JSON which is a hierarchical structure of key-value pairs: key->key->key->value.

Tried multiple ways to get the data seperate model property but data gets empty ! still the data am looking for a format of Dictionary<string , Dictionary<string,Dictionary<string,string>>> , The Json data is a seperate file which i read and then deserialise to the said format.

Note - tried : value with ["array item "] as well this works only for first key !

It appears your data is actually an array (or list) of items; you can tell due to the square brackets:

"PSQL": [
...
]

So, your structure should probably look something like this:

Dictionary<string, List<Dictionary<string, string>>>

That is, a dictionary with a string key and a list of dictionary (string, string) items. I used the Newtonsoft JSON package.

Hope this helps!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.