tags:

views:

63

answers:

2

Having an IList<NameValue> nameValueList, I need to convert that list to string for sending to aspx file as json. But because this happens in a project that does not have reference to system.web.script or system.web.mvc, i should use another way to serialize the IList

NameValue is an object that have 2 public properties (name and value)

A: 

This C# 4 snippet should serialize your collection to a JSON string:

"[" +
string.Join(",",
  from nv in list
  select string.Format("{{ name: {0}, value: {1} }}", nv.Name, nv.Value)
) + 
"]"
svick
Have you tried this for any realistic values of `nv.Name` or `nv.Value`? It would generate invalid JSON for almost anything containing any non-alphanumeric character, for example.
Timwi
Timwi, I think this answer should work fine. The question was to find a way to do it, not to find how to validate strings passed in JSON. That should be a separate question. Any other ideas? Giving +1.
Nayan
+1  A: 

What about just using Json.NET (and possibly Linq-to-Json)? http://json.codeplex.com/

Here's an example of Linq-to-Json: http://james.newtonking.com/archive/2008/02/11/linq-to-json-beta.aspx

mgroves