1

I tried to convert an anonymous object into a JSON object today but when I parse the result of the conversion it says the JSON is invalid. I am clueless why it does that. any help would be much appreciated. Cheers!

var s = new
{
    displayName = "ppplll",
        description = "Self help community for library",
        groupTypes = new[] { "Unified" },
        mailEnabled = true,
        mail = "[email protected]",
        mailNickname = "library",
        securityEnabled = false
};
JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize(s);

The json that gets created:

"{\"displayName\":\"ppplll\",\"description\":\"Self help community for library\",\"groupTypes\":[\"Unified\"],\"mailEnabled\":true,\"mail\":\"[email protected]\",\"mailNickname\":\"library\",\"securityEnabled\":false}"
6
  • Parse how? Who says that JSON is invalid? What json is produced? Commented Feb 27, 2017 at 9:20
  • parsed it at: jsonformatter.curiousconcept.com . Also included the Json in the post. Commented Feb 27, 2017 at 9:22
  • So that json is valid. Question remains: who says that it's invalid? Commented Feb 27, 2017 at 9:24
  • apparently after using @ciprian's answer worked. it is just a formatting difference between C# and the parsers i guess. Commented Feb 27, 2017 at 9:27
  • 1
    Well that string you probably got from debug view in Visual Studio. It displayed there as string literal, so all special characters (like quotes) are escaped with "\". The real string does not contain those slashes, only debug view (and even there you can click on dropdown button and choose "Text View" or "JSON view" to see plain text). Commented Feb 27, 2017 at 9:30

1 Answer 1

2

You should replace \" with " and then use http://jsonformatter.curiousconcept.com. The json is valid.

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

2 Comments

that worked. Could you please elaborate why C# uses the \" ? thanks for the info.
That string is actually a literal from debug. " marks the beginning/end of a string. When " character is actually part of the string (as is the case with json), it needs to be escaped using \. If you like to see the string without escaped ", use "Text visualizer".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.