前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#解析Json

C#解析Json

作者头像
ryzenWzd
发布2022-05-07 19:11:44
2.4K0
发布2022-05-07 19:11:44
举报
文章被收录于专栏:WPF

第一、解析JSON数据

工具:Newtonsoft.Json 类库/dll

目前我用到解析json数据的就只用到这个类库,用这个类库可以很方便的对于C#和JSON直接进行序列化和反序列化解析

首先我在本地文本文件txt复制了网上一段写好的json数据如下图(该txt文件路径是保存在d盘中):

好了,接下来我们就来解析这个txt文本格式的json数据吧!为了好演示我就直接新建一个控制台代码如下

代码语言:javascript
复制
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数据并且输出代码如下:

代码语言:javascript
复制
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();
            
        }
    }
}

在这个基础上代码再改下:

代码语言:javascript
复制
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 查询王力宏的朋友的名字:

代码语言:javascript
复制
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的解析就介绍到这里了。。。有什么不对的地方或者需要改正的地方请大牛们指正

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-03-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档