-1

I'm developing an ASPNetCore WebApi. In the get method I'm using Newtonsoft.Json and deserialize an generic object ( I'm not using a class model ) I get the response, works fine, but this have back slashes (in postman and in swagger)

[HttpGet]
[Route( "Obtener/{dwdocid:int}" )]
public IActionResult Obtener ( int dwdocid ) {

var dictionary = ( IDictionary<string, object> ) flexible; // for build the data
string json=null;

try {
    for ( int i = 1; i < item.Fields.Count - 1; i++ ) {
        // Taking data form another platform ( other system )*******
        // this platoform give us the data as strings
        // I make a diference between field and value for creating json response
        var field = item.Fields[i].FieldName.ToString();
        var value = item.Fields[i].Item == null ? "" : item.Fields[i].Item.ToString();
        dictionary.Add( field, value ); // building json
    }

} catch ( Exception error ) {

    return StatusCode( StatusCodes.Status500InternalServerError, new { mensaje = error.Message, response = JsonConvert.SerializeObject( dictionary ) } );
}
        

dk.desconectar( UriPlatform, dwUser, dwPass ); // diconnection
return StatusCode( StatusCodes.Status200OK, new { mensaje = "OK ;)", lista = JsonConvert.SerializeObject( dictionary ) } );
// return Json SerializeObject

}

enter image description here

I've converted te response in string for remove back slashes, but is not working.

How can I get a normal json response?? Please, I hope anyone can help me.

thanks.

angel_neo
  • 333
  • 2
  • 5
  • 22
  • 3
    The status code response is already serializing the response, and you're doing the extra serialization of the dictionary stored as lista. So lista ends up being a string. Try setting lista = dictionary directly so that only one serialization happens – emagers Aug 25 '23 at 21:43
  • You're right... Thanks a lot! – angel_neo Aug 25 '23 at 22:07

0 Answers0