美文网首页Node.js编程
.Net Core 优秀库及简单用法

.Net Core 优秀库及简单用法

作者: 任前程 | 来源:发表于2018-11-04 09:31 被阅读47次

工作中经常用到一些比较优秀的第三方库,这里收集整理了一些真实使用过的库及用法。有:Newtonsoft.Json、ReverseMarkdown、RestSharp

一、JSON字符串处理库:Newtonsoft.Json

官当网站:https://www.newtonsoft.com/json

NuGet地址:https://www.nuget.org/packages/Newtonsoft.Json/

1. 序列化

Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };

string json = JsonConvert.SerializeObject(product);
// {
//   "Name": "Apple",
//   "Expiry": "2008-12-28T00:00:00",
//   "Sizes": [
//     "Small"
//   ]
// }

2. 反序列化

定义类的方式:

string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";

Movie m = JsonConvert.DeserializeObject<Movie>(json);

string name = m.Name;
// Bad Boys

不定义类的方式:

string json3 = @"{
                    ""Name"": ""Jack"",
                    ""Age"": 34,
                    ""Colleagues"": [
                        {
                            ""Name"": ""Tom"",
                            ""Age"": 44
                        },
                        {
                            ""Name"": ""Abel"",
                            ""Age"": 29
                        }
                    ]
                }";
//将json转换为JObject 
JObject jObj = JObject.Parse(json3);
//通过属性名或者索引来访问
JToken ageToken = jObj["Age"]; 
JToken ageAbelToken = jObj["Colleagues"]["Age"]; //ageAbelToken.ToString();  //29
//修改--将jack年龄修改为35 
jObj4["Age"] = 35;
//针对没有的属性获取的值为null
var sss= jObj["ssss"]; //null

3. Linq 转Json

JArray array = new JArray();
array.Add("Manual text");
array.Add(new DateTime(2000, 5, 23));

JObject o = new JObject();
o["MyArray"] = array;

string json = o.ToString();
// {
//   "MyArray": [
//     "Manual text",
//     "2000-05-23T00:00:00"
//   ]
// }

二、Html 转 Markdown 库:ReverseMarkdown

Html转Markdown的库,对比过几种,目前来看就 ReverseMarkdown 表现最优秀。

官网地址:https://github.com/mysticmind/reversemarkdown-net

NuGet地址:https://www.nuget.org/packages/ReverseMarkdown/

1. 基本用法:

var converter = new ReverseMarkdown.Converter();
string html = "This a sample <strong>paragraph</strong> from <a href=\"http://test.com\">my site</a>";
string result = converter.Convert(html);
//result This a sample **paragraph** from [my site](http://test.com)

// with config
bool githubFlavored = true; // generate GitHub flasvoured markdown, supported for BR, PRE and table tags
bool removeComments = true; // will ignore all comments
var config = new ReverseMarkdown.Config(UnknownTagsOption.PassThrough, 
                githubFlavoured:githubFlavoured, removeComments:removeComments);
var converter = new ReverseMarkdown.Converter(config);

三、简单的REST和HTTP API 客户端:RestSharp

官网:http://restsharp.org/

NuGet地址:https://www.nuget.org/packages/RestSharp/

1. 基本用法

var client = new RestClient("https://www.yuque.com/api/v2/");
var request = new RestRequest(Method.GET);
request.AddHeader("x-auth-token", "xxxx9999cccc8");
IRestResponse response = client.Execute(request);


本文为原创文章,转载请注明出处!欢迎关注任前程博客 https://renqiancheng.com/,第一时间看后续精彩文章。

相关文章

网友评论

    本文标题:.Net Core 优秀库及简单用法

    本文链接:https://www.haomeiwen.com/subject/iekrxqtx.html