Retrieve value from Dictionary in c#

Binumon George 21 Reputation points
2022-03-04T17:06:50.96+00:00

Hi All,
Below i am giving one of the Dictionary<string, string > parameter value. from this how can i retrieve values of sStatus,sType,sStartDatetime,sEndDateTime

{"sItems":{"sStatus":"","sType":"all","sStartDatetime":"","sEndDateTime":""},"oTableParams":{"draw":1,"columns":

I know this json string not completed. But I need to retrieve value from this

Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-03-04T20:30:29.487+00:00

    json has few main value types

    null
    number
    string
    hashmap (string keyed dictionary of values)
    array of values

    your sample looks like a hashmap of hashmaps. these can be converted to Dictionay<string,object>

            dynamic foo = new Dictionary<string,object>
            {
                {
                    "sItems", 
                    new Dictionary<string,object>
                    {
                        {"sStatus", ""},
                        {"sType","all"},
                        {"sStartDatetime",""},
                        {"sEndDateTime",""}
                    }
                },
                {
                    "oTableParams", 
                    new Dictionary<string,object>
                    {
                        {"draw", 1},
                        {"columns",""}
                    }
                }
            };  
    
            Console.WriteLine(foo["oTableParams"]["draw"]);
    
    0 comments No comments

  2. Jack J Jun 25,296 Reputation points
    2022-03-07T02:46:21.313+00:00

    @Binumon George , based on your description, you want to read some values from json string.
    According to my test, I find that the json string you provided is not a valid string, so I changed into the following:

    {  
       "sItems":{  
          "sStatus":"ok",  
          "sType":"all",  
          "sStartDatetime":"2021-10-01",  
          "sEndDateTime":"2022-01-01"  
       },  
       "oTableParams":{  
          "draw":1,  
          "columns":""  
       }  
    }  
       
    

    Then, I stored it in the txt file and used the following code to read the value.

           var jsonString = File.ReadAllText(@"C:\Users\username\Desktop\1.txt");  
            var objects = JsonConvert.DeserializeObject<dynamic>(jsonString);  
            Console.WriteLine(objects.sItems.sStatus);  
            Console.WriteLine(objects.sItems.sType);  
            Console.WriteLine(objects.sItems.sStartDatetime);  
            Console.WriteLine(objects.sItems.sEndDateTime);  
    

    Result:

    180475-image.png

    Hope this could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and 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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.