文本格式与Json格式数据互相转换分享

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 ///导入命名空间LitJson
 8 using LitJson;
 9 namespace ConsoleApplication1
10 {
11     /// <summary>
12     /// 定义一个人物类
13     /// </summary>
14     public class Person
15     {
16         public string name;//名称
17         public int age;//年龄
18         public DateTime birthday;//生日
19     }
20     public class Program
21     {
22         static void Main(string[] args)
23         {
24             PersonToJson();
25             JsonToPerson();
26         }
27 
28         /// <summary>
29         ///文本转成Json格式
30         /// </summary>
31         public static void PersonToJson()
32         {
33             Person bill = new Person();
34             bill.name = "LiuKe";
35             bill.birthday = new DateTime(1997, 7, 30);
36 
37             string json_bill = JsonMapper.ToJson(bill);
38 
39             Console.WriteLine(json_bill);
40         }
41         /// <summary>
42         /// Json转成文本格式
43         /// </summary>
44         public static void JsonToPerson()
45         {
46             string json = @"{
47                              ""name"":""huangyi"",
48                              ""age"":20,
49                              ""birthday"":""02/07/1478 00:00:00""
50                             }";
51 
52             Person huangyi = JsonMapper.ToObject<Person>(json);
53             Console.WriteLine("name:{0},age:{1},birthday:{2}", huangyi.name, huangyi.age, huangyi.birthday);
54         }
55     }
56 }

 

转载于:https://www.cnblogs.com/mantianxin/p/7444811.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值