0

I'm posting the following json to the web.api:

{
    "tipoReporte":"039",
    "fecha":"20/05/2016",
    "datos":{
        "Prop1":"prop1",
        "Prop2":"prop2",
        "Prop3":"prop3",
        "Prop4":"prop4",
        "Prop5":"prop5",
        "Prop6":"prop6"
    },
    "usuarioID":2
}

What I need is that 'datos' is taken on the c# side as a string variable. I was not able to do that because when I debug the backend, I always see my variable 'Datos' filling with null value. Below is the model. Any idea of how I can map the json object to a string?. Thanks.

    public int Id { get; set; }
    public int TipoReporte { get; set; }
    public DateTime Fecha { get; set; }
    public string Datos { get; set; }
    public int UsuarioID { get; set; }
    public int Enviado { get; set; }
1
  • datos isn't a string. It's an object. You'd have to parse it on the server side as a sub object. Or, if you really must do this client side, then you'll have to serialize that object to another form, to allow it to be placed in this parameter as a string. Commented May 17, 2016 at 13:32

2 Answers 2

1

You can use a Dictionary<TKey, TValue> for that. Example:

public class MyObject
{
    public int Id { get; set; }

    [JsonProperty("tipoReporte")]
    public int TipoReporte { get; set; }

    [JsonProperty("fecha")]
    public DateTime Fecha { get; set; }

    [JsonProperty("datos")]
    public Dictionary<string, string> Datos { get; set; }

    [JsonProperty("usuarioID")]
    public int UsuarioID { get; set; }

    public int Enviado { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you want datos as a string just put quotes outside of brackets

{
        "tipoReporte":"039",
        "fecha":"20/05/2016",
        "datos": '{"Prop1":"prop1","Prop2":"prop2","Prop3":"prop3","Prop4":"prop4","Prop5":"prop5 "Prop6":"prop6" }',
        "usuarioID":2
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.