C#如和将List<>转为Json格式
3个回答
展开全部
StringBuilder sb = new StringBuilder();
List<string> ls = new List<string>();
ls.Add("百");
ls.Add("度");
ls.Add("知");
ls.Add("道");
ls.Add("行");
ls.Add("家");
System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
jss.Serialize(ls, sb);
MessageBox.Show(sb.ToString());
效果:
2017-06-15 · 知道合伙人互联网行家
关注
展开全部
Json数据格式:
var jsontext = "[{"id":"815bb899-8d70-4745-b799-7e68840a2b34","text":"设计","pid":"57eb2d57-9384-4d9f-a399-2c228fea81c2"},{"id":"4c76b72c-35da-4749-893c-5c7dde9431e6","text":"市政工程","pid":""}]";
/// <summary>
/// Json数据转数组
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonText"></param>
/// <returns></returns>
public List<T> JsonToList<T>(string jsonText)
{
List<T> list = new List<T>();
DataContractJsonSerializer _Json = new DataContractJsonSerializer(list.GetType());
byte[] _Using = System.Text.Encoding.UTF8.GetBytes(jsonText);
System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_Using);
_MemoryStream.Position = 0;
return (List<T>)_Json.ReadObject(_MemoryStream);
}
public class CheckedTreeInfo
{
/// <summary>
/// 父节点Id
/// </summary>
public string pid { get; set; }
/// <summary>
/// 节点Id
/// </summary>
public string id { get; set; }
/// <summary>
/// 节点名称
/// </summary>
public string text { get; set; }
}
var jsontext = "[{"id":"815bb899-8d70-4745-b799-7e68840a2b34","text":"设计","pid":"57eb2d57-9384-4d9f-a399-2c228fea81c2"},{"id":"4c76b72c-35da-4749-893c-5c7dde9431e6","text":"市政工程","pid":""}]";
/// <summary>
/// Json数据转数组
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonText"></param>
/// <returns></returns>
public List<T> JsonToList<T>(string jsonText)
{
List<T> list = new List<T>();
DataContractJsonSerializer _Json = new DataContractJsonSerializer(list.GetType());
byte[] _Using = System.Text.Encoding.UTF8.GetBytes(jsonText);
System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_Using);
_MemoryStream.Position = 0;
return (List<T>)_Json.ReadObject(_MemoryStream);
}
public class CheckedTreeInfo
{
/// <summary>
/// 父节点Id
/// </summary>
public string pid { get; set; }
/// <summary>
/// 节点Id
/// </summary>
public string id { get; set; }
/// <summary>
/// 节点名称
/// </summary>
public string text { get; set; }
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
List<int> nums = new List<int>();
nums.Add(1);
nums.Add(1);
nums.Add(1);
nums.Add(2);
nums.Add(2);
nums.Add(2);
nums.Add(2);
DataContractJsonSerializer json = new DataContractJsonSerializer(nums.GetType());
string szJson = "";
//序列化
using (MemoryStream stream = new MemoryStream())
{
json.WriteObject(stream, nums);
szJson = Encoding.UTF8.GetString(stream.ToArray());
}
Console.WriteLine(szJson);
nums.Add(1);
nums.Add(1);
nums.Add(1);
nums.Add(2);
nums.Add(2);
nums.Add(2);
nums.Add(2);
DataContractJsonSerializer json = new DataContractJsonSerializer(nums.GetType());
string szJson = "";
//序列化
using (MemoryStream stream = new MemoryStream())
{
json.WriteObject(stream, nums);
szJson = Encoding.UTF8.GetString(stream.ToArray());
}
Console.WriteLine(szJson);
更多追问追答
追问
List myClass= new List();也可以吧?
追答
可以的
class Program
{
static void Main(string[] args)
{
//Product.GetAllSmartPhones();
List nums = new List();
nums.Add(new Person() {
id=1,
name="sara"
});
nums.Add(new Person() {
id=1,
name="sylar"
});
DataContractJsonSerializer json = new DataContractJsonSerializer(nums.GetType());
string szJson = "";
//序列化
using (MemoryStream stream = new MemoryStream())
{
json.WriteObject(stream, nums);
szJson = Encoding.UTF8.GetString(stream.ToArray());
}
Console.WriteLine(szJson);
Console.ReadLine();
}
}
[DataContract]
class Person {
[DataMember]
public int id;
[DataMember]
public string name;
}
注意类上面的那个特性
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询