Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法
?Newtonsoft.JsonNewtonsoft.Json 是.Net平台操作Json的工具,他的介绍就不多说了,笔者最近在弄接口,需要操作Json。 以某个云计算平台的Token为例,边操作边讲解。 Json 转为 Model将 Model 转为 Json将 LINQ 转为 JSONLinq 操作命名空间、类型、方法大全另外附上 百度AI 文字识别 Json 及其模型类? ? ?Newtonsoft.Json 将字符串转为对象,是根据类型对象名称进行的,大小写不分,但是名称要一致要,哪怕你的json只有一个 { "a":1 } 你的对象 public class Test { int aa{get;set;} } 也是不能对应的。 有复杂层次的 json,可以使用 “类中类” 来映射,要注意 List<T>/Array/ArrayList的类型的使用。 ?Json 转为 Model? 新建一个?Json?文件,名字随意,例如 json1.json 把以下内容粘贴进去 { refresh_token": 25.ea2f85ebd48df85fe5400000.18640.282335-15533349",expires_in2592010session_key9mzdWr3n8ncMeDgX8zjhkhlW8khb5cdZtPevPbPwQGBg==access_token24.ac0ca9fakhlkyhl552017858.282335-15533349scopeaudio_voice_assistant_get audio_tts_post public vis-ocr_ocr nlp_simnet nlp_wclassify_watermark brain_ocr_scope vis-classify_car brain_gif_antiporn brain_ocr_general brain_ocr_general_basic brain_ocr_generer vis-classify_animal brain_politician brain_unit_utterance brain_imgquality_general brain_nlp_simnet brain_nlp_depparser vis-classify_plant brain_solution brain_ocr_plate_number brain_nlp_wordembedding brain_nlp_dnnlm_cn_legacy brain_nlp_simnet_legacy brain_nlp_commain_animal_classify brain_plant_classify brain_solution_iocr brain_realtime_product brain_nlp_lexer_custom brain_kgbody_analysis brain_body_attr brain_ocr_vat_invoice brain_advanced_general_classify brain_numbers brain_body_number vis-faceverify_FACE_auth_sessionkey smartapp_swanid_verify smartapp_opensource_openapisession_secret2ca66d464545c77a4767f709873be4 } ? 定义一个模型,文件名为?AccessTokenModel.cs AccessTokenModel { string refresh_token { get; ; } string expires_in { set; }//: Access Token的有效期(秒为单位,一般为1个月) string scope { string session_key { string access_token { : 要获取的Access Token string session_secret { ; } } 打开?Program.cs 文件 static void Main(string[] args) { FileStream fs = new FileStream(@"请修改成你的文件路径json1.jsonnew StreamReader(fs); string str = ""; line; while ((line = fileStream.ReadLine()) != null) { str += line; } ?重点方法? JsonConvert.DeserializeObject<要转化的模型类>("字符串对象"); 之后可以很方便的把Json文件的内容存放到数据库中。 集合把Json文件改成以下的样子 [{ },{ } ] ? @"请修改成你的文件路径json1.json line; } 上面的代码没有意义,只是将Json文件的内容加载到字符串中 JObject jObject = new JObject(); 新建 操作对象 List<AccessTokenModel> a = JsonConvert.DeserializeObject<List<AccessTokenModel>>(str); foreach (var i in a) { Console.WriteLine(i.access_token); } Console.ReadKey(); } ? 将Model转为Json能够将模型对象转为 Json。 继续使用上面的?AccessTokenModel.cs 文件, [] args) { AccessTokenModel accessTokenModel = AccessTokenModel(); accessTokenModel.access_token = test1; accessTokenModel.expires_in = test2; accessTokenModel.refresh_token = test3; accessTokenModel.scope = test4; accessTokenModel.session_key = test5; accessTokenModel.session_secret = test6; JObject jObject = JObject(); string str = JsonConvert.SerializeObject(accessTokenModel); //转为字符串 Console.WriteLine(str); Console.ReadKey(); } 重点方法? JsonConvert.SerializeObject(a模型对象); 运行后可以看到控制台输出的是Json字符串了,你可以继续把他放到Json文件中,这里不再赘述。 将 LINQ 转为 JSON下面这个是从官网直接copy的例子,Jarray 是其框架提供的一种类型。 在控制台运行后会发现输出的字符是已经格式化的。 [] args) { JArray array = JArray(); array.Add(Manual text); array.Add(new DateTime(2000,5,1)">23)); JObject o = JObject(); o[MyArray"] = array; string json = o.ToString(); { "MyArray": [ "Manual text", "2000-05-23T00:00:00" ] } Console.WriteLine(json); Console.ReadKey(); ? Linq 操作框架提供了对 Jobject 对象的Linq操作支持 using Newtonsoft.Json.Linq;
之后你可以像操作数组、集合或者Context一样方便。 ? 命名空间、类型、方法大全本来想翻译一下的,英语太差,算了。在常用的类型前面加粗吧 ? Classes
?
另外附上 百度AI 文字识别 Json 及其模型类?图片 笔者在弄了一段时间的百度 Ai 平台的SDK,封装了OCR SDK,由于现在在找实习工作,所以有部分没有弄完,有兴趣可以添加笔者的微信免费获取。微信在右侧导航栏。 百度AI 识别文字,返回Json结果,名字随意.格式建议为 json,如果使用记事本保存,注意编码格式是 utf-8,因为c# string默认为utf8,不然会乱码。 {
"log_id": 3413661945235258919,"direction": 0,"words_result_num": 2,"words_result": [
{
"vertexes_location": [
{
"y": 81,"x": 51
},{
"y": 81,"x": 151
},{
"y": 103,"x": 51
}
],"probability": {
"variance": 0.0,"average": 0.999861,"min": 0.999627
},"chars": [
{
"char": "今","location": {
"width": 17,"top": 83,"left": 60,"height": 20
}
},{
"char": "天","left": 78,{
"char": "除","location": {
"width": 12,"left": 103,{
"char": "了","location": {
"width": 16,"left": 116,{
"char": "皮","location": {
"width": 13,"left": 140,"height": 20
}
}
],"min_finegrained_vertexes_location": [
{
"y": 81,"finegrained_vertexes_location": [
{
"y": 81,"x": 71
},"x": 90
},"x": 110
},"x": 129
},"x": 149
},{
"y": 91,{
"y": 100,"x": 132
},"x": 112
},"x": 93
},"x": 73
},"x": 54
},{
"y": 93,{
"y": 84,"location": {
"width": 102,"top": 81,"left": 51,"height": 24
},"words": "今天除了皮"
},{
"vertexes_location": [
{
"y": 109,"x": 52
},{
"y": 109,"x": 152
},{
"y": 130,"x": 52
}
],"probability": {
"variance": 8E-05,"average": 0.9907,"min": 0.973259
},"chars": [
{
"char": "又","top": 111,"left": 61,{
"char": "啥","left": 85,{
"char": "也","left": 98,{
"char": "没","location": {
"width": 15,"left": 123,{
"char": "干","left": 141,"min_finegrained_vertexes_location": [
{
"y": 109,"finegrained_vertexes_location": [
{
"y": 109,"x": 91
},{
"y": 119,{
"y": 129,"x": 133
},"x": 113
},"x": 94
},"x": 74
},"x": 55
},{
"y": 121,{
"y": 111,"top": 109,"left": 52,"height": 22
},"words": "又啥也没干"
}
],"language": -1
}
对应的模型 ,将 cs 文件,名字??GeneralModel.cs /// <summary> /// 通用文字识别(含位置版)返回结果 </summary> GeneralModel { <summary> 必选 唯一的log id,用于问题定位 </summary> long log_id { ; } 图像方向,当detect_direction=true时存在。 非必选 - -1:未定义, - 0:正向, - 1: 逆时针90度, - 2:逆时针180度, - 3:逆时针270度 int direction { ; } 识别结果数,表示words_result的元素个数 int words_result_num { 检测语言 默认值会返回 -1 string language { 定位和识别文字结果数组 public List<Words_result> words_result { ; } Words_result { <summary> 图片中文字段四个顶点位置(矩形范围) </summary> public List<XY> vertexes_Location { ; } 可选 行置信度信息;如果输入参数 probability = true 则输出 public Probability probability { 每个字 public List<Chars> chars { 最小细粒度顶点坐标 public List<XY> min_finegrained_vertexes_location { 细粒度顶点坐标,多边形 public List<XY> finegrained_vertexes_location { 文字在图片中的相对位置 public Location location { 识别出的文字 string words { 坐标 XY { int x { ; } int y { ; } } 行置信度 Probability { <summary> 行置信度平均值方差 </summary> double variance { ; } 行置信度平均值 double average { ; } 行置信度最小值 double min { 单个文字 Chars { 识别的单个文字 char chaR { 该文字范围(矩形) ; } } } Location { int left { ; } int top { int width { int height { ; } } } } 可用控制台进行检验 [] args) { StreamReader streamReader = new StreamReader(System.IO.File.OpenRead(json文件位置)); ; jsonstr; while ((jsonstr = streamReader.ReadLine()) != ) { str += jsonstr; } GeneralModel generalModel = JsonConvert.DeserializeObject<GeneralModel>(str); Console.WriteLine(图片id:" + generalModel.log_id); Console.WriteLine(图像方向: generalModel.direction); Console.WriteLine(检测语言为: generalModel.language); Console.WriteLine(有几个结果: generalModel.words_result_num); var item generalModel.words_result) { Console.WriteLine(识别结果: Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(item.words))); var itemi item.vertexes_Location) { Console.WriteLine({x:" + itemi.x + ;y:" + itemi.y + }); } Console.WriteLine(Probability:可信度:" + 行置信度平均值" + item.probability.average + ;行置信度平均值方差:" + item.probability.variance + ;行置信度平均值最小值: item.probability.min); item.chars) { Console.WriteLine(itemi.chaR); Console.WriteLine(位置: left:" + itemi.location.left + ; height: " + itemi.location.height + top: " + itemi.location.top + ; width: itemi.location.width); } } Console.ReadKey(); ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |