locked
Getting values from Json object RRS feed

  • Question

  • User-1961814522 posted

    Hi,

    I have a Json structure like this

    {\"sender\":{\"name\":\"Ashvin\",\"email\":\"astrapolo@gmail.com\"},\"to\":[{\"email\":\"astrapolo@hotmail.com\",\"name\":\"Ashvin Hotmail\"}],\"attachment\":[{\"content\":\"iVBORw0KGgoAAAANSUhEUgAmCC\",\"name\":\"MyProfilePicture.jpeg\"}],\"textContent\":\"This is sent with attachment\",\"subject\":\"test subject\"}

    I would like to get the values in the Json through C# code like SenderName, SenderEmail, ToEmail, ToName TextContent etc.

    Is there a way to achieve this through Dot Net.

    Thanks,

    Ashvin

    Friday, October 2, 2020 6:08 PM

All replies

  • User-2054057000 posted

    You include the Newtonsoft.Json package from NuGet then use it's DeserializeObject method to convert the json to a C# class object. Refer this tutorial which get's a json from an api and then converts the json data to a c# object.

    Hope it help you in what you are looking

    Saturday, October 3, 2020 1:02 PM
  • User711641945 posted

    Hi ashvinvee,

    I would like to get the values in the Json through C# code like SenderName, SenderEmail, ToEmail, ToName TextContent etc.

    Not sure what do you mean,if you just want to get the value of specific property,here is a working demo:

    var source = "{\"sender\":{\"name\":\"Ashvin\",\"email\":\"astrapolo@gmail.com\"},\"to\":[{\"email\":\"astrapolo@hotmail.com\",\"name\":\"Ashvin Hotmail\"}],\"attachment\":[{\"content\":\"iVBORw0KGgoAAAANSUhEUgAmCC\",\"name\":\"MyProfilePicture.jpeg\"}],\"textContent\":\"This is sent with attachment\",\"subject\":\"test subject\"}";
    dynamic data = JObject.Parse(source);
    Console.WriteLine(data.sender.name);
    Console.WriteLine(data.sender.email);
    Console.WriteLine(data.to[0].email);
    Console.WriteLine(data.to[0].name);
    Console.WriteLine(data.textContent);

    Result:

    Best Regards,

    Rena

    Monday, October 5, 2020 7:20 AM