文本格式與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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM