关键词搜索

源码搜索 ×
×

Windows CE /SmartDeviceProject 操作Json数据实现对象序列化和反序列化

发布2014-01-21浏览3763次

详情内容

      使用场景:实现Java对象的Json数据在C#中读取,重新封装为C#的对象,数据传递采用webservice方式,所有返回类型为int 、boolean 、String、long 等基础数据类型,避免传输复杂的java List和自定义对象;而String的格式就是熟悉的Json。 访问资源下载路径:http://jsoncf.codeplex.com/

源代码和编译后文件下载地址:http://jsoncf.codeplex.com/releases/view/18199?RateReview=true

在线代码阅读地址:http://jsoncf.codeplex.com/SourceControl/latest#ReadMe.txt

1、引用程序包


2、编写实体对象

  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using CodeBetter.Json;
  6. namespace SmartDeviceProjectWtms.Domain
  7. {
  8. [SerializeIncludingBase]
  9. class Demo
  10. {
  11. private int _id;
  12. private String _name;
  13. public int id {
  14. get { return _id; }
  15. set { _id = value;}
  16. }
  17. public String name {
  18. get { return _name;}
  19. set { _name = value;}
  20. }
  21. public Demo() {
  22. }
  23. public Demo(int _id,String _name)
  24. {
  25. id = _id;
  26. name = _name;
  27. }
  28. }
  29. }
注:必须引用可序列化注解

3、反序列化封装工具

  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.ComponentModel;
  6. using SmartDeviceProjectWtms.Domain;
  7. using CodeBetter.Json;
  8. namespace SmartDeviceProjectWtms
  9. {
  10. class JsonUtil
  11. {
  12. public static string ReplaceSpecialChar(String jsonStr)
  13. {
  14. jsonStr = jsonStr.Replace("\\t", " ");
  15. jsonStr = jsonStr.Replace("null", "\"" + "null" + "\"");
  16. return jsonStr;
  17. }
  18. /************************************************测试例子**********************************/
  19. /// <summary>
  20. /// 获取Demo属性的封装对象【例子】
  21. /// </summary>
  22. /// <param name="jsonStr">Json对象字符串集合</param>
  23. /// <returns></returns>
  24. public static Demo GetDemoByJsonStr(String jsonStr)
  25. {
  26. Demo demo = Converter.Deserialize<Demo>(jsonStr,"_");
  27. return demo;
  28. }
  29. /// <summary>
  30. /// 获取Demo属性的封装对象【例子】
  31. /// </summary>
  32. /// <param name="jsonStr">Json对象字符串集合</param>
  33. /// <returns></returns>
  34. public static List<Demo> GetDemoListByJsonStr(String jsonStr)
  35. {
  36. List<Demo> list = Converter.Deserialize<List<Demo>>(jsonStr, "_");
  37. return list;
  38. }
  39. }
  40. }
注:ReplaceSpecialChar方法是处理Java javax.util.Date对象和值为空null的Json字符问题。

4、编写反序列测试类

  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Windows.Forms;
  5. using SmartDeviceProjectWtms.Domain;
  6. using CodeBetter.Json;
  7. namespace SmartDeviceProjectWtms
  8. {
  9. class JsonTest
  10. {
  11. /// <summary>
  12. /// 测试【JSON单个对象】反序列化
  13. /// </summary>
  14. public static void TestDemo2()
  15. {
  16. string jsonStr =@"{""id"":1,""name"":""boonya""}";
  17. MessageBox.Show("{}JSon:" + jsonStr);
  18. Demo demo = JsonUtil.GetDemoByJsonStr(jsonStr);
  19. if (demo != null)
  20. {
  21. MessageBox.Show("{}获取Object:" + demo.id + " " + demo.name);
  22. }
  23. }
  24. /// <summary>
  25. /// 测试【JSON对象集合】反序列化
  26. /// </summary>
  27. public static String TestDemo3() {
  28. string jsonStr = @"[{""id"":1,""name"":""boonya""}]";
  29. List<Demo> dList = JsonUtil.GetDemoListByJsonStr(jsonStr);
  30. if (dList != null) {
  31. MessageBox.Show("[]JSon:" + jsonStr);
  32. foreach (Demo demo in dList)
  33. {
  34. MessageBox.Show("[]获取Object:" + demo.id + " " + demo.name);
  35. }
  36. }
  37. return jsonStr;
  38. }
  39. }
  40. }
注:本例只是一个简单的例子,具体Java对象转过来的Json处理可能更为复杂,但处理基本一样。

5、Json包程序序列化和反序列化操作例子

  1. namespace CodeBetter.Json.Test.Console
  2. {
  3. using System;
  4. internal class Program
  5. {
  6. private static void Main(string[] args)
  7. {
  8. string json = Converter.Serialize(new User("name", "password", AccountStatus.Enabled));
  9. Converter.Serialize("out.txt", new int[] { 1, 2, 3, -4 }, "_");
  10. Console.WriteLine(json);
  11. User user = Converter.Deserialize<User>(json, "_");
  12. int[] values = Converter.DeserializeFromFile<int[]>("out.txt", "_");
  13. Console.WriteLine(user.UserName);
  14. Console.WriteLine("Done. Press enter to exit");
  15. Console.ReadLine();
  16. }
  17. }
  18. public class BaseUser
  19. {
  20. private int _id = 1;
  21. }
  22. [SerializeIncludingBase]
  23. public class User : BaseUser
  24. {
  25. private string _userName;
  26. private string _password;
  27. [NonSerialized]
  28. private readonly Role _role;
  29. private AccountStatus _status;
  30. private Thing _think = new Thing();
  31. public string UserName
  32. {
  33. get { return _userName; }
  34. set { _userName = value; }
  35. }
  36. public string Password
  37. {
  38. get { return _password; }
  39. set { _password = value; }
  40. }
  41. public AccountStatus Status
  42. {
  43. get { return _status; }
  44. set { _status = value; }
  45. }
  46. public Role Role
  47. {
  48. get { return _role; }
  49. }
  50. public Thing Thing
  51. {
  52. get { return new Thing(); }
  53. }
  54. public User(string userName, string password, AccountStatus status)
  55. {
  56. UserName = userName;
  57. Password = password;
  58. Status = status;
  59. _role = new Role(DateTime.Now, "Admin", this);
  60. }
  61. private User()
  62. {
  63. }
  64. }
  65. public class Role
  66. {
  67. public Role(DateTime expires, string name, User user)
  68. {
  69. Expires = expires;
  70. Name = name;
  71. User = user;
  72. }
  73. public DateTime Expires { get; set; }
  74. public string Name { get; set; }
  75. public User User { get; set; }
  76. public Thing Thing
  77. {
  78. get { return new Thing(); }
  79. }
  80. }
  81. public class Thing
  82. {
  83. private string _name = "ABC";
  84. public string Name
  85. {
  86. get { return _name; }
  87. set { _name = value; }
  88. }
  89. }
  90. public enum AccountStatus
  91. {
  92. Enabled = 1,
  93. Disabled = 2,
  94. }
  95. }




相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载