using Newtonsoft.Json; //VS2013引入: project->Add Reference->Extensions->Json.NET
namespace JsonProject
{
//Json示例,
//string str = new Type1().ToJson(); //{"name":"李明","年龄":23}
//Type1 obj = Type1.Parse(str); //从json串创建Type1对象
class Type1
{
public string name;
[JsonProperty("年龄")] //为age重命名
public int age;
[JsonIgnore] //忽略该属性
public string address;
public Type1()
{
name = "李明";
age = 23;
address = "地址xxxx";
}
//将当前对象Type1的数据,转化为Json串
public String ToJson()
{
return JsonConvert.SerializeObject(this);
}
//从Json串创建Type1对象
public static Type1 Parse(string JsonStr)
{
return JsonConvert.DeserializeObject<Type1>(JsonStr);
}
// 数组的反序列化,返回Type1数组List
public static List<Type1> Iteams(string JsonStr)
{
List<Type1> iteams = JsonConvert.DeserializeObject<List<Type1>>(JsonStr);
return iteams;
}
}
}
C# Json示例
最新推荐文章于 2024-04-17 08:50:31 发布