How to read data from Json object

Murari Ram 21 Reputation points
2021-12-18T14:45:56.607+00:00

Hi All,

I am getting the data as below. Please help me how to read the data of Head_TO_Res_Det and Head_TO_Pos_Det

string jsonString = "{\"d\":{\"Head_TO_Res_Det\":{\"results\":[{\"Status\":\"E\",\"Task\":\"\",\"Pernr\":\"17015263\",\"Reason\":\"Error - Employee currently in Bench\"}]},\"Head_TO_Pos_Det\":{\"results\":[]},\"LvText\":\"\",\"Pernr\":\"00000000\"}}";

Waiting for valuable replies

Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2021-12-20T07:27:37.603+00:00

    @Murari Ram , you could try the following code to get data from json object.

     string jsonString = "{\"d\":{\"Head_TO_Res_Det\":{\"results\":[{\"Status\":\"E\",\"Task\":\"\",\"Pernr\":\"17015263\",\"Reason\":\"Error - Employee currently in Bench\"}]},\"Head_TO_Pos_Det\":{\"results\":[]},\"LvText\":\"\",\"Pernr\":\"00000000\"}}";  
                dynamic stuff = JObject.Parse(jsonString);  
                Console.WriteLine(stuff.d.Head_TO_Res_Det.results);  
                foreach (JObject item in stuff.d.Head_TO_Res_Det.results)  
                {  
                    foreach (JProperty jp in item.Properties())  
                    {  
                        Console.WriteLine(jp.Name);  
                        Console.WriteLine(jp.Value);  
                        Console.WriteLine("*****");  
                    }  
                }  
                Console.WriteLine(stuff.d.Head_TO_Pos_Det);  
      
                Console.ReadKey();  
    

    Please note that we need to install nuget-package Newtonsoft.Json first of all.

    Result:

    158943-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.


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.