0

I have an ASP.NET Core 6 Web API controller. I am trying to return a mix of JSON strings along with plain C# objects as the result of one of controller's web methods. But I am facing a few problems.

The following web method can produce the problems I am facing:

[HttpGet("GetMixedJson")]
public IActionResult GetMixedJson()
{
    string[] Summaries = new[]
    {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

    string json = JsonConvert.SerializeObject(Summaries, Formatting.Indented);

    JObject jsonObject = new JObject();
    jsonObject["thisFiledValueWillBeMissing"] = "JObject won't serialize :(";

    var resultObject = new
    {
        PlainCSharpFiled = "plain c# fields showing fine",
        jsonObject = jsonObject,
        TunneledJsonString = json
    };

    return new JsonResult(resultObject);
}

It produces the following JSON response:

{
  "plainCSharpFiled": "plain c# fields showing fine",
  "jsonObject": {
    "thisFiledValueWillBeMissing": []
  },
  "tunneledJsonString": "[\r\n  \u0022Freezing\u0022,\r\n  \u0022Bracing\u0022,\r\n  \u0022Chilly\u0022,\r\n  \u0022Cool\u0022,\r\n  \u0022Mild\u0022,\r\n  \u0022Warm\u0022,\r\n  \u0022Balmy\u0022,\r\n  \u0022Hot\u0022,\r\n  \u0022Sweltering\u0022,\r\n  \u0022Scorching\u0022\r\n]"
}

Problem #1: JObject jsonObject value is missing

Problem #2: The json data in TunneledJsonString is encoded, hence it is corrupted in the result, not usable.

Please note that I simplified the code. In my project, the actual objects data fields are coming from variety of sources and they are bigger. The more I save on memory cost the better.

What are my options to fix the problems #1 and #2?

1 Answer 1

1

in .net 6 web api projects,serialize and deserialize operations are based on System.Text.Json

You could add Newtonsoft JSON format support with this package:

Microsoft.AspNetCore.Mvc.NewtonsoftJson

and configure as below :

builder.Services.AddControllers().AddNewtonsoftJson();

The result:

enter image description here

object Summaries = new[]
            {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

            

            string json = JsonConvert.SerializeObject(Summaries);

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

But the Problem #2 remains outstanding. Is there a better way of addressing Problem #2?
""was not encoded to \u0022 any more ,what do you want ? delete \r\n and make it looks like:["Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"] ? or like that i've updated in my answer?
Thank you for help. I think I mixed two problems in one question. Let me post another question and elaborate Problem #2.

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.