首页 > 代码库 > c#操作json 使用JavaScriptSerializer
c#操作json 使用JavaScriptSerializer
需要引用:System.Web.Extensions
/// <summary> /// json的信息。保证定义的变量和json的字段一样(也可以使用struct) /// </summary> public class JsonInfo { public string res_code { get; set; } public string res_info { get; set; } public List<Persion> persionList; } public class Persion { public string name { get; set; } public string age { get; set; } } public class OperateJson { public static void ParseJson() { string json = "{‘res_code‘:‘0‘,‘res_info‘:‘ok‘,‘persionList‘:[{‘name‘:‘ladygaga‘,‘age‘:‘50‘},{‘name‘:‘swift‘,‘age‘:‘42‘}]}"; JavaScriptSerializer js = new JavaScriptSerializer(); JsonInfo jsonInfo = js.Deserialize<JsonInfo>(json); } public static string CreateJson(object objectList) { JavaScriptSerializer js = new JavaScriptSerializer(); string json = js.Serialize(objectList); return json; } }
使用
private void button3_Click(object sender, EventArgs e) { OperateJson.ParseJson(); } private void button4_Click(object sender, EventArgs e) { JsonInfo jsonInfo = new JsonInfo(); jsonInfo.res_code = "0"; jsonInfo.res_info = "ok"; jsonInfo.persionList = new List<Persion>(); Persion persion = new Persion(); persion.name = "lady gaga"; persion.age = "50"; jsonInfo.persionList.Add(persion); Persion persion1 = new Persion(); persion1.name = "swift"; persion1.age = "42"; jsonInfo.persionList.Add(persion1); OperateJson.CreateJson(jsonInfo); }
c#操作json 使用JavaScriptSerializer
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。