亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

c# Newtonsoft.Json 常用方法總結(jié)

 更新時間:2021年02月19日 15:13:26   作者:丹楓無跡  
這篇文章主要介紹了c# Newtonsoft.Json 常用方法的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下

1 實體類的 Json 序列化和反序列化

我們以如下的 Person 類舉例,其中包含了常用的數(shù)據(jù)類型:

public class Person
{
 public int ID { get; set; }

 public string Name { get; set; }

 public DateTime Birthday { get; set; }

 public bool IsVIP { get; set; }
 
 public float Account { get; set; }

 public string[] Favorites { get; set; }
 
 public string Remark { get; set; }
}

創(chuàng)建一個 Person 實例:

Person person = new Person
{
 ID = 1,
 Name = "張三",
 Birthday = DateTime.Parse("2000-01-02"),
 IsVIP = true,
 Account = 12.34f,
 Favorites = new string[] { "吃飯", "睡覺" }
};

1.1 Json 序列化

返回不縮進(jìn)的 Json 字符串

JsonConvert.SerializeObject(person);

{"ID":1,"Name":"張三","Birthday":"2000-01-02T00:00:00","IsVIP":true,"Account":12.34,"Favorites":["吃飯","睡覺"],"Remark":null}

返回縮進(jìn)的 Json 字符串

JsonConvert.SerializeObject(person, Formatting.Indented);

{
 "ID": 1,
 "Name": "張三",
 "Birthday": "2000-01-02T00:00:00",
 "IsVIP": true,
 "Account": 12.34,
 "Favorites": [
  "吃飯",
  "睡覺"
 ],
 "Remark": null
}

1.2 將不縮進(jìn)的 JSON 字符串轉(zhuǎn)成縮進(jìn)形式

private string JsonIndentation(string str)
{
 //string str = JsonConvert.SerializeObject(entity);
 JsonSerializer serializer = new JsonSerializer();
 TextReader tr = new StringReader(str);
 JsonTextReader jtr = new JsonTextReader(tr);
 object obj = serializer.Deserialize(jtr);
 if (obj != null)
 {
  StringWriter textWriter = new StringWriter();
  JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
  {
   Formatting = Formatting.Indented,
   Indentation = 4,
   IndentChar = ' '
  };
  serializer.Serialize(jsonWriter, obj);
  return textWriter.ToString();
 }
 else
 {
  return str;
 }
}

或者:

private string JsonIndentation(string json)
{
 JObject obj = JObject.Parse(json);
 return obj.ToString();
}

1.3 其他設(shè)置

JsonSerializerSettings settings = new JsonSerializerSettings();
// 設(shè)置日期格式
settings.DateFormatString = "yyyy-MM-dd";
// 忽略空值
settings.NullValueHandling = NullValueHandling.Ignore;
// 縮進(jìn)
settings.Formatting = Formatting.Indented;

JsonConvert.SerializeObject(person, settings);

返回:

{
 "ID": 1,
 "Name": "張三",
 "Birthday": "2000-01-02",
 "IsVIP": true,
 "Account": 12.34,
 "Favorites": [
 "吃飯",
 "睡覺"
 ]
}

1.4 Json 反序列化

JsonConvert.DeserializeObject<Person>(json);

2 JObject 使用

2.1 創(chuàng)建對象

JObject obj = new JObject();
obj.Add("ID", 1);
obj.Add("Name", "張三");
obj.Add("Birthday", DateTime.Parse("2000-01-02"));
obj.Add("IsVIP", true);
obj.Add("Account", 12.34f);
// 創(chuàng)建數(shù)組
JArray array = new JArray();
array.Add(new JValue("吃飯"));
array.Add(new JValue("睡覺"));
obj.Add("Favorites", array);
obj.Add("Remark", null);

2.2 JObject 中添加數(shù)組

上例中的代碼可以簡化為:

JArray array = new JArray("吃飯", "睡覺");

2.3 從 Json 字符串創(chuàng)建 JObject

string json = "{\"ID\":1,\"Name\":\"張三\",\"Birthday\":\"2000-01-02T00:00:00\",\"IsVIP\":true,\"Account\":12.34,\"Favorites\":[\"吃飯\",\"睡覺\"],\"Remark\":null}";

JObject obj = JObject.Parse(json);

2.4 從 Entity 創(chuàng)建 JObject

JObject obj = JObject.FromObject(person);

用匿名對象創(chuàng)建 JObject

JObject obj = JObject.FromObject(new { name = "jack", age = 18 });

//顯示
{
 "name": "jack",
 "age": 18
}

用初始化器

JObject obj = new JObject()
{
 { "name", "jack" },
 { "age", 18 }
};

2.5 獲取值

int id;
if (obj["ID"] != null)
 id = obj["ID"].Value<int>();

2.6 獲取數(shù)組

Newtonsoft.Json.Linq 不支持直接獲取數(shù)組,但是可以獲取 List,然后再轉(zhuǎn)化為數(shù)組。

string[] favorites;
if (obj["Favorites"] != null)
 favorites = obj["Favorites"].Value<List<string>>().ToArray();

以上就是c# Newtonsoft.Json 常用方法總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于c# Newtonsoft.Json的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#基于正則表達(dá)式刪除字符串中數(shù)字或非數(shù)字的方法

    C#基于正則表達(dá)式刪除字符串中數(shù)字或非數(shù)字的方法

    這篇文章主要介紹了C#基于正則表達(dá)式刪除字符串中數(shù)字或非數(shù)字的方法,涉及C#針對數(shù)字的簡單正則匹配相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06
  • unity實現(xiàn)繪畫功能

    unity實現(xiàn)繪畫功能

    這篇文章主要為大家詳細(xì)介紹了unity實現(xiàn)繪畫功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • WinForm之BindingSource基礎(chǔ)操作實例教程

    WinForm之BindingSource基礎(chǔ)操作實例教程

    這篇文章主要介紹了WinForm之BindingSource基礎(chǔ)操作,對BindingSource組建的用法進(jìn)行較為深入的實例分析,需要的朋友可以參考下
    2014-08-08
  • WPF自定義路由事件的實例教程

    WPF自定義路由事件的實例教程

    &#65279;WPF中有兩種事件模型,一種是在WinForm時代就存在的CLR事件,另一種是WPF時代的路由事件,這篇文章主要給大家介紹了關(guān)于WPF自定義路由事件的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • c#文件下載示例的4種方法分享

    c#文件下載示例的4種方法分享

    這篇文章主要介紹了c#文件下載示例的4種方法,有TransmitFile實現(xiàn)下載,WriteFile實現(xiàn)下載,WriteFile分塊下載,流方式下載,需要的朋友可以參考下
    2014-03-03
  • C#關(guān)于System.Collections空間詳解

    C#關(guān)于System.Collections空間詳解

    這篇文章主要介紹了C#關(guān)于System.Collections空間,需要的朋友可以參考下
    2014-07-07
  • c#生成縮略圖的實現(xiàn)方法

    c#生成縮略圖的實現(xiàn)方法

    c#生成縮略圖的實現(xiàn)方法,需要的朋友可以參考一下
    2013-04-04
  • 使用revit api畫垂直于風(fēng)管的風(fēng)管示例

    使用revit api畫垂直于風(fēng)管的風(fēng)管示例

    這篇文章主要介紹了使用revit api畫垂直于風(fēng)管的風(fēng)管示例,需要的朋友可以參考下
    2014-03-03
  • C#圖片壓縮的實現(xiàn)方法

    C#圖片壓縮的實現(xiàn)方法

    一般在web應(yīng)用中,對客戶端提交上來的圖片肯定需要進(jìn)行壓縮的。尤其是比較大的圖片,如果不經(jīng)過壓縮會導(dǎo)致頁面變的很大,打開速度比較慢,當(dāng)然了如果是需要高質(zhì)量的圖片也得需要生產(chǎn)縮略圖。
    2013-02-02
  • 詳解C#的排列組合

    詳解C#的排列組合

    本文詳細(xì)介紹了C#中的排列組合以及具體實現(xiàn)代碼,如下所示,希望對大家有所幫助
    2016-11-11

最新評論