1

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 '\' enter image description here

Extra '\' enter image description here

How can i remove these '\' ?

5
  • 1
    Can you paste your controller code so we can see what you're doing? Commented Dec 20, 2013 at 12:40
  • This is an artifact from QuickWatch. Know your tools and don't solve problems that aren't there. Commented Dec 20, 2013 at 12:45
  • @HenkHolterman: I understand that when we see data in quick watch, extra '\' is for display purpose. But even the output sent via to the client contains the "\". When I look at the other objects which i have serialized into json, they do not have extra '\' Commented Dec 20, 2013 at 12:59
  • What does the JS code make of it? I'm still not convinced there is a actual problem here. If it deserializes then all is fine. Commented Dec 20, 2013 at 13:07
  • @HenkHolterman: Even I am not sure if this is the actual problem. I take this JSON and pass it to a widget which display the output. check this link : - fiddle.sencha.com/#fiddle/1v5 But now when the JSON output is having extra '\', I do not see any data in the grid. Commented Dec 20, 2013 at 13:21

4 Answers 4

3

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 :)

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

2 Comments

I see. Problem is I am returning a composite object and added JSON object (serialized root) into this composite object as a string property. I cannot return only root object. It has to be a part of another composite object.
@Brown_Dynamite: That doesn't matter - just return the parent object, which has a field with the root. Why shouldn't that work? Don't combine JSON on your own, you're just going to make a ton of mistakes. Leave it up to the serializer, that's his job :P
3

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.

1 Comment

Thank for reply. I see your point. Problem is I am returning a composite object and added JSON object (serialized root) into this composite object as a string property. I cannot return only root object. It has to be a part of another composite object.
0

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.

Comments

-1

You can apply on your javascript:

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

This should convert "\\" into "\"

1 Comment

Thank you for reply Bardo. But want to understand why these '\' are coming & how can I remove them (not by replacing them)

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.