3

I am planning to do a JSON Response from an ASP.NET Core Controller but it is not as simple as I thought.

A simple string like {"ConfigValue":"192.168.1.125:1880"} should be serialized to a JsonObject and this object shall be part of the JSON Response.

But what the return Json(str) response is something like

{\u0022ConfigValue\u0022:\u0022192.168.1.125:1880\u0022}

I have no clue how to get rid of \u0022

public class commonController : Microsoft.AspNetCore.Mvc.Controller
{
    private  IConfiguration config;

    public commonController(IConfiguration configuration)
    {
        config = configuration;
    }
    // GET
    [Route("getConfigEntry/{key?}")]
    public JsonResult getConfigEntry(string? key)
    {
        Dictionary<string, string> dict = new Dictionary<string, string>
        {
            {"ConfigValue", "192.168.1.125:1880"}
        };
        str = JsonConvert.SerializeObject(dict,Formatting.None); //from debugger variable viewer {"ConfigValue":"192.168.1.125:1880"}
        return Json(str); // "{\u0022ConfigValue\u0022:\u0022192.168.1.125:1880\u0022}"
    }
}
1
  • 9
    Man, you gotta celebrate this day. You asked your first question and the greatest Jon Skeet answered it Commented Dec 22, 2020 at 23:21

2 Answers 2

12

You're serializing into JSON twice - first you're serializing the dictionary into a string, then you're serializing that string as a JSON value... which involves escaping quotes.

You should only serialize it once - you don't have to call JsonConvert.SerializeObject at all:

return Json(dict);
Sign up to request clarification or add additional context in comments.

Comments

0

I encountered this error when I was creating the jsonstring with the following fomart with key and value sorrounded by double quotes

@"{
        ""product"": {
            ""x"": ""123456"",
            ""y"": 0
        }

I then solved it by using inbuilt function to create json instance that can be sent over the wire wrapped in an HttpResponseMessage, whereby jsonBody is a POCO model/class

JsonContent.Create(jsonBody, options: new JsonSerializerOptions { DictionaryKeyPolicy = JsonNamingPolicy.CamelCase })

3 Comments

This is not an answer to this question (, which already go a good answer over 3 years ago). Please don't put vaguely related knowledge in random answers. And read How to Answer.
It may sound vague to you but this helped solved this problem when I encountered it. It might help other people who encountered the problem like me if it hasn't helped you.
Thank you for your effort and attempt, but that's just not how Stack Overflow works. There are tens of millions of questions and answers on stack overflow. The issue you actually faces has probably already been asked somewhere. That you found "a" not-really-related question that inspired you to find your own solution, doesn't mean you should add your solution to you problem as an answer to that non-related question. It just makes it harder for people to 'filter out' actual answers they are looking for.

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.