0

I am making a call to a webservice and getting following Json Respons

{"handler":{"name":"abc"},"intent":{"name":"actions.intent.MAIN","params":{},"query":"Mit Google sprechen"},"scene":{"name":"actions.scene.START_CONVERSATION","slotFillingStatus":"UNSPECIFIED","slots":{},"next":{"name":"Start_Frage"}},"session":{"id":"ABwppHHVumDrliLJaLSikS6KnIlN7yYv6Z4XJCOYzEZt8Fr08RH6r0wtM2-E0v40lS2p1YosTDfpSCd5Lw","params":{},"typeOverrides":[],"languageCode":""},"user":{"locale":"de-DE","params":{},"accountLinkingStatus":"ACCOUNT_LINKING_STATUS_UNSPECIFIED","verificationStatus":"VERIFIED","packageEntitlements":[],"gaiamint":"","permissions":[],"lastSeenTime":"2021-04-01T10:06:59Z"},"home":{"params":{}},"device":{"capabilities":["SPEECH","RICH_RESPONSE","LONG_FORM_AUDIO"]}}

I used https://json2csharp.com/ to convert my Json String to C# Classes

 public class Handler
    {
        public string name { get; set; }
    }

    public class Params
    {
    }

    public class Intent
    {
        public string name { get; set; }
        public Params @params { get; set; }
        public string query { get; set; }
    }

    public class Slots
    {
    }

    public class Next
    {
        public string name { get; set; }
    }

    public class Scene
    {
        public string name { get; set; }
        public string slotFillingStatus { get; set; }
        public Slots slots { get; set; }
        public Next next { get; set; }
    }

    public class Session
    {
        public string id { get; set; }
        public Params @params { get; set; }
        public List<object> typeOverrides { get; set; }
        public string languageCode { get; set; }
    }

    public class User
    {
        public string locale { get; set; }
        public Params @params { get; set; }
        public string accountLinkingStatus { get; set; }
        public string verificationStatus { get; set; }
        public List<object> packageEntitlements { get; set; }
        public string gaiamint { get; set; }
        public List<object> permissions { get; set; }
        public DateTime lastSeenTime { get; set; }
    }

    public class Home
    {
        public Params @params { get; set; }
    }

    public class Device
    {
        public List<string> capabilities { get; set; }
    }

    public class Root
    {
        public Handler handler { get; set; }
        public Intent intent { get; set; }
        public Scene scene { get; set; }
        public Session session { get; set; }
        public User user { get; set; }
        public Home home { get; set; }
        public Device device { get; set; }
    }

But how exactly do I parse my Json respone into an C# Object? Then make any changes to It and finally send a response back? Im a newbie in programming thats why a Step by Step example would be very helpful

My current class looks like this. Variable body holds the Json response.

public class GoogleController : ControllerBase
    {

        [HttpGet]
        public IActionResult Get()
        {
            var result = new Result();
            result.Value1 = 123;

            return Ok(result);
        }
        [HttpPost]
        public async Task<IActionResult> PostWebHook()
        {

            string body;
            using (var reader = new StreamReader(Request.Body))
            {
                body = await reader.ReadToEndAsync();

            }
            return Ok("Test123");
                
        }
    }
3
  • 1
    How about using JsonConvert.DeserializeObject? Commented Apr 6, 2021 at 8:51
  • 1
    You don't have to explicitly deserialize the request content. If the action accepts an object that matches the JSON body, ASP.NET will deserialize it automatically Commented Apr 6, 2021 at 9:02
  • 3
    Perhaps it would be a good idea to run through some basic MVC tutorials. This is pretty fundamental to how it works. Commented Apr 6, 2021 at 9:02

2 Answers 2

5

Use FromBody attribute to deserialize body

    [HttpPost]
    public async Task<IActionResult> PostWebHook([FromBody] Root root)
    {
        // root is deserialized body
        // modify root
        ...
        return Ok(root);
    }
Sign up to request clarification or add additional context in comments.

Comments

2

https://json2csharp.com/ adds a command in the first line something like:

Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 

This line is used to deserialize the text.

Yours should be like:

var myDeserializedClass = JsonConvert.DeserializeObject<Root>(body);

2 Comments

This is very manual and relies on JSON.Net being used. Instead, just let the framework do this for you, as the other answer shows.
@DavidG you are right arther's answer is better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.