JSON

C#序列化和反序列化综合案例(4)

字号+ 作者:H5之家 来源:H5之家 2016-05-08 14:06 我要评论( )

下面是用到的方法 Reading the specified file that contains a json string ( object sender, EventArgs e){ //Judge whether the specified file exists. if (File.Exists(filename)){ //Getting the string that

下面是用到的方法

Reading the specified file that contains a json string (object sender, EventArgs e) { //Judge whether the specified file exists. if (File.Exists(filename)) { //Getting the string that is a specified file using (StreamReader sr = new StreamReader(filename, System.Text.Encoding.UTF8)) { string json = sr.ReadToEnd(); //analyze the json string. txtOrgJson.Text = json; List<Person> people = jss.Deserialize<List<Person>>(json); StringBuilder sb = new StringBuilder(); PropertyInfo[] piArr = typeof(Person).GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (people.Count == 0) { DeserializeToObject(json); return; } //Get the name and value of the specified class Person automatically. foreach (var person in people) { sb.Clear(); foreach (PropertyInfo pi in piArr) { sb.Append(pi.Name + "=" + pi.GetValue(person)); sb.Append("\t "); } listAll.Items.Add(sb.ToString()); //listAll.Items.Add("name=" + person.Name + "\tid=" + person.Id + "\tphone=" + person.Phone); } } } else { MessageBox.Show("Cannot find the specified file.Please click the up button of this."); } } 6.总结以及如何移植到web和其他序列化途径 先写这么多,你有什么疑问也可以留言交流一下,该案例仅供参考,不代表标准案例。另外,由于这里用的是WinForm开发,你在Web里通过POST方法传入json数据时,可能需要通过下面的方法获取到json string StreamReader reader = new StreamReader(context.Request.InputStream); //比如得到json字符串:strJson={"key3":"xdp-gacl","key4":"白皇"} String strJson = HttpUtility.UrlDecode(reader.ReadToEnd());

然后下面就是按上面的案例中的方法进行优化,调整。
另外呢,如果是传入的JSON string是一个,那么可以通过键值对的方式来读取:

/// <summary> /// 获取参数 /// </summary> /// <param name="context"></param> /// <returns></returns> private System<String, Object> GetParameter(HttpContext context) { StreamReader reader = new StreamReader(context.Request.InputStream); //得到json字符串:strJson={"key3":"xdp-gacl","key4":"白虎神皇"} String strJson = HttpUtility.UrlDecode(reader.ReadToEnd()); Systemjss = new System(); //将json字符串反序列化成一个Dictionary对象 System<String, Object> dicParameter = jss<String, Object>>(strJson); return dicParameter; }

调用方式

Dictionary<String, Object> dicParameter = GetParameter(context); string key3 = dicParameter["keyword"].ToString();

另外呢,除了上面的序列化之外,还可以用下面的方法:

(object item) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(item.GetType()); using (MemoryStream ms = new MemoryStream()) { serializer.WriteObject(ms, item); StringBuilder sb = new StringBuilder(""); sb.Append(Encoding.UTF8.GetString(ms.ToArray())); return sb.ToString(); } }

需要你引用:using System.Runtime.Serialization.Json;

好,暂时就分享到这里。谢谢!

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • C++ Builder 里面使用 JsonCpp 和 JsonCpp 支持 UNICODE (UTF

    C++ Builder 里面使用 JsonCpp 和 JsonCpp 支持 UNICODE (UTF

    2016-04-08 15:00

  • 详解C++的JSON静态链接库JsonCpp的使用方法

    详解C++的JSON静态链接库JsonCpp的使用方法

    2016-04-02 14:00

  • C++的Json解析库:jsoncpp

    C++的Json解析库:jsoncpp

    2016-01-26 09:22

  • jsoncpp 库的c++实现,里面还有测试程序,是不可多得的学习资料 Li

    jsoncpp 库的c++实现,里面还有测试程序,是不可多得的学习资料 Li

    2016-01-14 19:29

网友点评
c