问题描述
比方说,我有一个对象MyObject来,看起来像这样:
Let's say I have an object MyObject that looks like this:
public class MyObject
{
int ObjectID {get;set;}
string ObjectString {get;set;}
}
我有myObject的名单,我期待将它转换成一个JSON字符串一个StringBuilder。我知道如何创建一个JavascriptConverter,并通过将列表,并具有变流器建立字符串,但在这种特殊情况下,我希望避免的开销,直接进入一个JSON字符串列表中的foreach循环创建一个JSON字符串像这样的:
I have a list of MyObject and I'm looking to convert it in a json string with a stringbuilder. I know how to create a JavascriptConverter and create a json string by passing a list and having the converter build the string but in this particular case I'm looking to avoid the overhead and go straight to a json string with a foreach loop on the list like this:
StringBuilder JsonString = new StringBuilder();
foreach(MyObject TheObject in ListOfMyObject)
{
}
我试过用逗号和引号追加使用这种方法,但它并没有制定出(还)。
I've tried to use this method by appending with commas and quotes but it hasn't worked out (yet).
感谢您的建议。
推荐答案
我已经做了一些喜欢使用前的JavaScript序列化类:
I've done something like before using the JavaScript serialization class:
using System.Web.Script.Serialization;
和
JavaScriptSerializer jss = new JavaScriptSerializer();
string output = jss.Serialize(ListOfMyObject);
Response.Write(output);
Response.Flush();
Response.End();
这篇关于转换列表以JSON格式 - 快速简便的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!