简体   繁体   中英

Extra '\' is comign while converting C# object into JSON using JSON.NET

I am converting C# list into JSON using JSON.NET library. After converting the object when I look into quick watch I can see extra '\\' with every property. I am sending this data via a controller (asp.net MVC controller) to one of JavaScript client. When the data is sent, data is having extra '\\'. How can I remove these extra '\\'?

my controller:

public class MyController : myBase
    {
        public string Get(string id = null, string userName = null)
        {
            List<Data> dataList = new List<Data>();

            Data d = new Data();
            d.Name = "FireFox";
            d.Folder = @"Testing\Mac OSX";
            dataList.Add(d);

            d = new Data();
            d.Name = "Safari";
            d.Folder = @"Testing\Mac OSX";
            dataList.Add(d);

            d = new Data();
            d.Name = "Chrome";
            d.Folder = @"Testing\Mac OSX";
            dataList.Add(d);

            d = new Data();
            d.Name = "FireFox";
            d.Folder = @"Testing\Windows";
            dataList.Add(d);

            d = new Data();
            d.Name = "Safari";
            d.Folder = @"Testing\Windows";
            dataList.Add(d);

            d = new Data();
            d.Name = "Chrome";
            d.Folder = @"Testing\Windows";
            dataList.Add(d);

            d = new Data();
            d.Name = "Internet Exploder";
            d.Folder = @"Testing\Windows";
            dataList.Add(d);


            d = new Data();
            d.Name = "Chrome";
            d.Folder = @"Testing\Linux";
            dataList.Add(d);

            d = new Data();
            d.Name = "Firefox";
            d.Folder = @"Testing\Linux";
            dataList.Add(d);

            d = new Data();
            d.Name = "Testing First Child";
            d.Folder = @"Testing";
            dataList.Add(d);

            d = new Data();
            d.Name = "First Child";
            d.Folder = null;
            dataList.Add(d);


            Node root = new Node();
            foreach (Data da in dataList)
            {
                Node parent = root;
                if (!string.IsNullOrEmpty(da.Folder))
                {
                    Node child = null;
                    foreach (string part in da.Folder.Split(new char[] {'\\'}, StringSplitOptions.RemoveEmptyEntries))
                    {
                        string name = part.Trim();
                        child = parent.children.Find(n => n.Name == name);
                        if (child == null)
                        {
                            child = new Node {Name = name};
                            parent.children.Add(child);
                        }
                        parent = child;
                    }
                }
                //Always adds the leaf node.
                parent.children.Add(new Node {Name = da.Name});
            }

            string output = JsonConvert.SerializeObject(root);
            return output;

        }

    }

    public class Data
    {
        public string Name { get; set; }
        public string Folder { get; set; }
    }

    class Node
    {
        public Node()
        {
            children = new List<Node>();
        }

        public List<Node> children { get; set; }
        public string Name { get; set; }
        public bool leaf { get; set; }
        public bool expanded { get; set; }
    }

data in firefox --- other controllers are returning data without '\\' 在此处输入图片说明

Extra '\\' 在此处输入图片说明

How can i remove these '\\' ?

ASP.Net Web API should do the JSON conversion for you, so you don't need to convert your return object to a string and return it. You can just return the object you want sent back as JSON (in this case, root ). Your method then just becomes:

public Node Get(string id = null, string userName = null)

Your client side calling code, as Chris W mentioned, needs to set the content-type, or accept header field to "application/json", so that MVC Web API knows to send data back in JSON format.

You're serializing the object in JSON, and then you're returning this string through the method - now it's a string representation of the object. The JSON web service API then takes this string, and encodes it to make sure that it is a valid JS string - hence the escaping on all the quotes.

Basically, you're doing double encoding. Instead of a string, return the root object directly, and all will be just fine :)

You can apply on your javascript:

yourStringJSON.replace(/\\\\/g, "\\");

This should convert "\\\\" into "\\"

While you are calling JsonConvert.SerializeObject, you then return a string from the controller - this has the effect of escaping the quote marks because the content type isn't "application/json".

Could you not use JsonResult, or inherit from JsonResult if you need to do extra processing before sending the Json data back?

If this isn't possible, you'd need to replace the escape characters on the client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM