Read a value inside a complex JSON in C#

Asked

Viewed 2,142 times

2

Good morning folks! I’m new to C# and I’m having a hard time picking up a value within a complex Json. This is Json:

 {
  "id": "58e50b82-50b1-4f29-a2e8-a9a544013255",
  "timestamp": "2018-06-20T18:00:12.935Z",
  "lang": "pt-br",
  "result": {
    "source": "agent",
    "resolvedQuery": "me conte uma novidade",
    "action": "",
    "actionIncomplete": false,
    "parameters": {
      "news": "novidade"
    }
}

So far I can collect only the first 3 Json data (id, timestamp and lang) with the following code:

API class:

[HttpPost]
[Route("consumindoApi")]
public HttpResponseMessage DirecionarResposta(Dialogflow dialogflow)
{
  try
  {
      return Request.CreateResponse(HttpStatusCode.OK,  
                    @"ID: " + dialogflow.id + 
                    ". Idioma: " + dialogflow.lang + 
                    ". ID da Sessão: " + dialogflow.sessionId + 
                    ". Horario da mensagem: " + dialogflow.timestamp);
   }
   catch (Exception e)
   {
      return Request.CreateResponse(HttpStatusCode.BadRequest, e.Message);
   }
 }

Model class I use to map Json:

public class Dialogflow
    {
        public string id { get; set; }
        public string timestamp { get; set; }
        public string lang { get; set; }
        public string sessionId { get; set; }
        public string result { get; set; }
    }

I use Newtonsoft to do this manipulation, but since I’m new to the piece, I can’t collect the data inside the list result and parameters. How would I manage to collect the values of the fields within these two lists?

  • You must create another object to result.

1 answer

3


result is another object, can not be string. Moreover, parameters is also an object. The structure would be like this:

public class Parameters
{
    public string news { get; set; }
}

public class Result
{
    public string source { get; set; }
    public string resolvedQuery { get; set; }
    public string action { get; set; }
    public bool actionIncomplete { get; set; }
    public Parameters parameters { get; set; }
}

public class Dialogflow
{
    public string id { get; set; }
    public DateTime timestamp { get; set; }
    public string lang { get; set; }
    public Result result { get; set; }
}

It is also possible to do without creating the classes, using dynamic, that way:

 dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

Take the example: https://dotnetfiddle.net/3UnSgD

More about dynamic: csharp/language-Reference/Keywords/Dynamic

  • Okay, so in the controller I’d have to call the parameter that way? List<Result> data = Jsonconvert.Deserializeobject<List<Result>() and List<Parameters> data = Jsonconvert.Deserializeobject<List<Parameters>>()

  • It does not need, as the object is only one, just convert to Dialogflow: var obj = JsonConvert.DeserializeObject<Dialogflow>(json);

  • Now if there’s a list, yes it would be <List<Dialogflow>>

  • Got it, in case this json value would be a variable of the method for receiving the string? Directional){...}

  • That’s right. It’s also possible to do without creating the classes, using dynamic, that way: dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(json); I’ll put in the answer

  • Damn, that dough! Expensive ball show, thanks for the help!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.