
C#解析Json
发布日期:2021-05-09 06:18:04
浏览次数:3
分类:博客文章
本文共 3412 字,大约阅读时间需要 11 分钟。
第一、解析JSON数据
工具:Newtonsoft.Json 类库/dll
目前我用到解析json数据的就只用到这个类库,用这个类库可以很方便的对于C#和JSON直接进行序列化和反序列化解析
首先我在本地文本文件txt复制了网上一段写好的json数据如下图(该txt文件路径是保存在d盘中):
好了,接下来我们就来解析这个txt文本格式的json数据吧!为了好演示我就直接新建一个控制台代码如下
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Newtonsoft.Json;using System.IO;using Newtonsoft.Json.Linq;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { JsonSerializer serializer = new JsonSerializer(); using (StreamReader reader=new StreamReader(@"d:\json.txt")) using (JsonReader jsreader=new JsonTextReader(reader)) { JObject jo = (JObject)serializer.Deserialize(jsreader);//对于json的反序列化再转化为JObject Console.WriteLine(jo.ToString()); } Console.Read(); } }}
运行结果:
二、Linq To Json
linq to json的主要类:
1.JObejct :用于操作json的对象
2.JArray: 用来操作json数组
3.JValue :数组中的值
4.JProperty: json对象的属性,通常以key和value类似于字典形式存在
5.JToken :用于存放linq查询的结果值
下面稍微演示下用法,用JObejct创建json数据并且输出代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Newtonsoft.Json;using System.IO;using Newtonsoft.Json.Linq;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { JObject jo = new JObject(); jo.Add(new JProperty("name", "张惠妹")); jo.Add(new JProperty("sex", "女")); jo.Add(new JProperty("age", "32")); jo.Add(new JProperty("teachar", new JObject(new JProperty("name", "张雨生"), new JProperty("sex", "男"), new JProperty("age", "30")))); Console.WriteLine(jo.ToString()); Console.ReadLine(); } }}
在这个基础上代码再改下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Newtonsoft.Json;using System.IO;using Newtonsoft.Json.Linq;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { JObject jo = new JObject(); jo.Add(new JProperty("name", "张惠妹")); jo.Add(new JProperty("sex", "女")); jo.Add(new JProperty("age", "32")); jo.Add(new JProperty("teachar", new JObject(new JProperty("name", "张雨生"), new JProperty("sex", "男"), new JProperty("age", "30")))); JToken Tteacher = jo["teachar"]; Console.WriteLine(Tteacher.ToString()); Console.ReadLine(); } }}
由此可以知道JToken返回的其实是对应key的value部分,再来用linq to json,首先准备一字符串,用linq 查询王力宏的朋友的名字:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Newtonsoft.Json;using System.IO;using Newtonsoft.Json.Linq;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { string jsonstr= "{\"Name\" : \"王力宏\", \"Age\" : 34, \"friends\" : [{\"Name\" : \"林俊杰\" , \"Age\":30},{\"Name\" : \"张惠妹\",\"Age\":29}] }"; JObject jo = JObject.Parse(jsonstr); var a = from b in jo["friends"].Children() select (string) b["Name"]; foreach (var item in a) { Console.WriteLine(item); } Console.Read(); } }}
简单地对于Json的解析就介绍到这里了。。。有什么不对的地方或者需要改正的地方请大牛们指正
转载地址:https://www.cnblogs.com/ryzen/p/8529915.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2023年09月30日 22时00分32秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Flask框架学习(1)——Hello World!
2019-03-08
APP渗透——安装包签名和证书
2019-03-08
Pycharm一直报No module named 'requests'
2019-03-08
putty提示Access denied
2019-03-08
vue-cli如何解决跨域?
2019-03-08
记一次vue--NavMenu--左侧导航栏动画效果折腾记
2019-03-08
vue-cli-+-element-ui-树形表格(多级表格折腾小计)
2019-03-08
vueCli-封装Echarts图表组件
2019-03-08
vue-spa-单页面应用跳转打开新的页面
2019-03-08
stl源码分析——map/multimap
2019-03-08
Effective C++条款06:若不想使用编译器自动生成的函数,就该明确拒绝
2019-03-08
Effective C++条款11:在operator=中处理“自我赋值”
2019-03-08
Effective C++条款15:在资源管理类中提供对原始资源的访问
2019-03-08
c++11总结08——noexcept关键字
2019-03-08
Effective C++条款17:以独立语句将newed对象置入智能指针
2019-03-08
Effective C++条款18:设计与声明——让接口容易被正确使用,不易被误用
2019-03-08
Effective C++条款19:设计与声明——设计class犹如设计type
2019-03-08
c++——struct和class的区别
2019-03-08
c++——类成员的访问权限
2019-03-08