C # development, the List & amp; lt; Object & amp; gt; sequence of the array is converted to the JSON, or to save transmission.

Generally, we often use an array using self-defined List to place our data, we may sometimes want the objects in the List save it for next time the program is turned on in the development of C # applications, but in Win store app or Windows Phone app development to provide the Local Folder and Roaming Folder when storing data not only into the List array into a simple String int variables of class double bool, so we can try to convert the List into JSON text, for storage or transfer to another place in these spaces.

This article will guide you through an array of serialized to JSON conversion List


Generally, we often use when developing C # applications Custom List 数组来放置我们的数据,或许有时我们会想要将List<>内的对象存起来以供下次程序开启时使用,但是在Win store app 或Windows Phone app 开发时提供的Local Folder 和 Roaming Folder存放数据时并不能放入List<>的数组只能放入简单的String int double bool 之类的变量,所以我们可以尝试将List<>转换为JSON的文字,以便存放在这些空间里或是传送到其他地方。

本篇文章将引导您 将List 中的数组序列化转换为JSON

在这之前要有事前准备在[项目]→[管理NeGet套件]中下载Json.NET的套件。

我们要自定义一个类,就以学生的姓名和身高为例。

   1:  public class student_Item {
   2:      public string name { set; get; }//姓名
   3:      public int  tall { set; get; }//身高
   4:  }

再来主程序内建立假数据

   1:  //建立假数据
   2:  List
     
     
      
       student_Items = new List
      
      
       
       ();
      
      
     
     
   3:  student_Items.Add(new student_Item() { name = "小美", tall = 166 });
   4:  student_Items.Add(new student_Item() { name = "小强", tall = 185 });
   5:  student_Items.Add(new student_Item() { name = "小明", tall = 148 });
   6:  student_Items.Add(new student_Item() { name = "小琪", tall = 186 });
   7:  student_Items.Add(new student_Item() { name = "小程", tall = 174 });
   8:  student_Items.Add(new student_Item() { name = "小杨", tall = 165 });

建完假数据后最重要的一行程序补上,该行程序便是将List序列化成JSON格式的字符串

   1:  string json_data = JsonConvert.SerializeObject(student_Items);//存放序列后的文字

整个程序看起来就会像这样子

   1:  List
     
     
      
       student_Items = new List
      
      
       
       ();
      
      
     
     
   2:  student_Items.Add(new student_Item() { name = "小美", tall = 166 });
   3:  student_Items.Add(new student_Item() { name = "小强", tall = 185 });
   4:  student_Items.Add(new student_Item() { name = "小明", tall = 148 });
   5:  student_Items.Add(new student_Item() { name = "小琪", tall = 186 });
   6:  student_Items.Add(new student_Item() { name = "小程", tall = 174 });
   7:  student_Items.Add(new student_Item() { name = "小杨", tall = 165 });
   8:   
   9:  string json_data = JsonConvert.SerializeObject(student_Items);//存放序列后的文字

json_data里面的数据就会长得像这样了,是经过序列化后的JSON格式数据!!

[ { "name" : "小美",
    "tall" : 166
  },
  { "name" : "小强",
    "tall" : 185
  },
  { "name" : "小明",
    "tall" : 148
  },
  { "name" : "小琪",
    "tall" : 186
  },
  { "name" : "小程",
    "tall" : 174
  },
  { "name" : "小杨",
    "tall" : 165
  }
]

如此一来我们便学会了如何将List<>转换为JSON格式的数据啰!

文章中的叙述如有观念不正确错误的部分,欢迎告知指正 谢谢
转载请注明出处,并且附上本篇文章网址 !  感谢。

SUKI

HOLIESTAR

https://www.facebook.com/Holiestar
https://www.facebook.com/firestar.register

原文:大专栏  C# 开发,将List&lt;Object&gt; 中的数组序列化转换为JSON,以便保存或传送。


posted on 2019-09-10 11:52 老特特傻 阅读(...) 评论(...) 编辑 收藏

导航

Guess you like

Origin www.cnblogs.com/chinatrump/p/11496656.html