jsonasp.net-core-webapi.net-core-3.1choetl

Convert string with escape characters in json object in .net core 3.1


con.Open();
MySqlParameter _ReturnValue = new MySqlParameter("_ReturnValue", MySqlDbType.Int32);
_ReturnValue.Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add(_ReturnValue);
using (var parser = new ChoJSONWriter(sb))
parser.Write(cmd.ExecuteReader());

Now what sb.ToString() is returning is: "[\n {\n \"code\": \"SC0009\"\n }\n]"

and just wanted to convert this to an object, for the same in .net core 2.1, below code was working fine:

private JToken ParseStrtoJson(string strv)
    {
        JToken token = JToken.Parse(@strv);
        return token;
    }

After upgrade to .net Core 3.1, it is returning what I receive in webapi result is this:

"code": [
        [
            [
                []
            ]
        ]
    ]

Which is quite not clear with and how to handle it.


Solution

  • In asp.net core 3.1, for the operation of json class, Microsoft.AspNetCore.Mvc.NewtonsoftJson needs to be installed.

    And add the following to the ConfigureServices method of Startup.cs:

       public void ConfigureServices(IServiceCollection services)
        {
           services.AddControllers().AddNewtonsoftJson();
        }
    

    You can refer to this.