开发者

How to serialize IList<object> to string

开发者 https://www.devze.com 2023-01-18 18:28 出处:网络
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 refere

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)


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)
) + 
"]"


What about just using Json.NET (and possibly Linq-to-Json)?

Here's an example of Linq-to-Json

0

精彩评论

暂无评论...
验证码 换一张
取 消