2

I have a handler that uploads a KML file and returns JSON with the KML file as an attribute:

context.Response.Write("{\"name\":\"" + FileName + 
"\",\"type\":\"" + FileType + 
"\",\"size\":\"" + FileSize + 
"\",\"region_id\":\"" + regionID + 
"\",\"kml\":\"" + HttpUtility.HtmlEncode(xmlData) + "\"}");

As you can see, I'm trying to encode the KML with HttpUtility.HtmlEncode but I get an error in my response:

uncaught exception: Invalid JSON

How can I property encode the XML/KML file in C# so I can later decode it in JavaScript?

Edit #1: per Cheeso's comment I'm using ASP.NET, .NET Version 4 on IIS 7.5 Windows 7. My handler is a ashx file. The response works fine if I leave out the KML data (HttpUtility.HtmlEncode(xmlData)) from the response.

Edit #2 I also tried using System.Web.Script.Serialization.JavaScriptSerializer per the moderator's comment. I used it like such:

System.Web.Script.Serialization.JavaScriptSerializer serializer;
context.Response.Write("{\"name\":\"" + FileName + 
"\",\"type\":\"" + FileType + 
"\",\"size\":\"" + FileSize + 
"\",\"region_id\":\"" + regionID + 
"\",\"kml\":\"" + serializer.Serialize(xmlData) + "\"}");

I still get the same "Invalid JSON" error.

16
  • 1
    Why not use a proper JSON encoding library? A list is here: json.org Commented May 26, 2011 at 17:50
  • 8
    Why wouldn't you use JavaScriptSerializer or JSON.NET for this? Commented May 26, 2011 at 17:50
  • 3
    @Pekka oh C# doesn't at all - nor can it handle XML, regex, or even DateTime. Good job that the .NET BCL has, though :) Commented May 26, 2011 at 17:51
  • 3
    @capdragon right; now that I'm back off mobile, I have added an example. Frankly, your tone above was both offensive and inappropriate. If you don't choose to use an object-level serializer that it up to you, but it is (as shown) a very valid answer to this problem. Especially considering that you haven't guarded the calling client by ensuring your own data is correctly encoded (which may or may not be a problem, depending on the candidate values of FileName, FileType etc). Commented May 26, 2011 at 19:57
  • 3
    It's @Marc by the way; I don't mind either-way, but I won't get a notification alert from @Mark. It is unfortunate that you took offence - none was intended. Commented May 26, 2011 at 22:45

2 Answers 2

10

You want to build JSON, right... and apparently it is ridiculous of me to suggest a JSON serializer.... yet:

string FileName = "foo.txt", FileType = "csv";
int FileSize = 1134, regionID = 12;
string xml = "<foo><bar/></foo>";

string json= new JavaScriptSerializer().Serialize(new {
    name = FileName,
    type = FileType,
    size = FileSize,
    region_id = regionID,
    kml = xml
});

In the majority of cases, using a pre-canned serializer is both more convenient and more robust against edge-cases of data.

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

4 Comments

Sorry, you misunderstood the question i don't want to build JSON... i have JSON, i want to encode xml within the json safely.
@capdragon - no, I understood the question - I just proposed an alternative (simpler, IMO) way of implementing it that also addresses the encoding issue
@Mark - okay man, you're right, you're the big moderator MVP king of all kings and GOD of stackoverflow. One line of code is simpler than what you have up there. i don't need to encode a string i need your serialization solution because there can't be anything better. Are you happy? is that what you want to hear?
@capdragon I don't understand your attitude; my intent is merely to present another suitable answer. I'm not telling you which option to take, and I'm genuinely delighted that the other answer gives you what you need. But I'm also thinking of the next reader, who might not take the same decision. I have used both forms of JSON construction - both are entirely valid in different scenarios. What is your complaint with my presenting the other option? I'm not forcing it on you. Seriously: lighten up.
4

An HTML Encoder encodes < as &lt; and so forth. That doesn't help you get XML into JSON format. What you want is a JavaScript Encode. Use HttpUtility.JavaScriptStringEncode

http://msdn.microsoft.com/en-us/library/dd991914.aspx

1 Comment

That's exactly what i needed and worked! thanks! All the other ridiculous comments from people were how to serialize .net objects into JSON +1

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.