C#使用Newtonsoft.Json中的JObject对象

案例1

json

{     "Name": "Jack",     "Age": 34,     "Colleagues": [{         "Name": "Tom",         "Age": 44     }, {         "Name": "Abel",         "Age": 29     }] }

代码

using Newtonsoft.Json.Linq; using System; namespace JObject案例 {     class Program     {         static void Main(string[] args)         {             string json = "{\"Name\" : \"Jack\", \"Age\" : 34, \"Colleagues\" : [{\"Name\" : \"Tom\" , \"Age\":44},{\"Name\" : \"Abel\",\"Age\":29}] }";             JObject jObject1 = JObject.Parse(json);             string name = jObject1["Name"].ToString();             string age = jObject1["Age"].ToString();             string colleagues1_name = jObject1["Colleagues"][0]["Name"].ToString();             string colleagues1_age = jObject1["Colleagues"][0]["Age"].ToString();             Console.WriteLine(name);             Console.WriteLine(age);             Console.WriteLine(colleagues1_name);             Console.WriteLine(colleagues1_age);             Console.ReadKey();         }     } }

运行

案例2

json

{     "ID": 1,     "Name": "张三",     "Favorites": ["吃饭", "睡觉"] }

代码

using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; namespace JObject案例 {     class Program     {         static void Main(string[] args)         {             string json = "{\"ID\":1,\"Name\":\"张三\",\"Favorites\":[\"吃饭\",\"睡觉\"]}";             JObject jObject1 = JObject.Parse(json);             Console.WriteLine(jObject1["ID"]);             Console.WriteLine(jObject1["Name"]);             Console.WriteLine(jObject1["Favorites"][0]);             Console.WriteLine(jObject1["Favorites"][1]);             Console.ReadKey();         }     } }

运行

案例3

json

{     "input": {         "size": 193156,         "type": "image/png"     },     "output": {         "size": 59646,         "type": "image/png",         "width": 487,         "height": 284,         "ratio": 0.3088,         "url": "https://www.baidu.com"     } }

代码

using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.IO; using System.Text; namespace JObject案例 {     class Program     {         static void Main(string[] args)         {             string json = "{\"input\":{\"size\":193156,\"type\":\"image/png\"},\"output\":{\"size\":59646,\"type\":\"image/png\",\"width\":487,\"height\":284,\"ratio\":0.3088,\"url\":\"https://www.baidu.com\"}}";             JObject jObject1 = JObject.Parse(json);             Console.WriteLine(jObject1["input"]["size"]);             Console.WriteLine(jObject1["input"]["type"]);             Console.WriteLine(jObject1["output"]["size"]);             Console.WriteLine(jObject1["output"]["type"]);             Console.ReadKey();         }     } }

运行

案例4

json

{     "code": "SUCCESS",     "msg": null,     "data": [{         "id": 31783735,         "residentInfoId": 2000099151,         "doctorId": "89bd0716-f916-4e51-93f7-4d416830f03c"     }] }

代码

using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.IO; using System.Text; namespace JObject案例 {     class Program     {         static void Main(string[] args)         {             string json = "{\"code\":\"SUCCESS\",\"msg\":null,\"data\":[{\"id\":31783735,\"residentInfoId\":2000099151,\"doctorId\":\"89bd0716-f916-4e51-93f7-4d416830f03c\"}]}";             JObject jObject1 = JObject.Parse(json);             Console.WriteLine(jObject1["code"]);             Console.WriteLine(jObject1["SUCCESS"]);             Console.WriteLine(jObject1["data"][0]["id"]);             Console.WriteLine(jObject1["data"][0]["residentInfoId"]);             Console.WriteLine(jObject1["data"][0]["doctorId"]);             Console.ReadKey();         }     } }

运行

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对易知道(ezd.cc)的支持。如果你想了解更多相关内容请查看下面相关链接

推荐阅读

    foreach的用法c语言和c#

    foreach的用法c语言和c#,数组,遍历,本文目录foreach的用法c语言和c#详细讲解foreach循环的用法C#中的foreach 怎么用啊foreach用法C#中的f

    SQLite使用JSON扩展

    SQLite使用JSON扩展,插件,加载,一、介绍 SQLite3.9.0之后的版本,添加了JSON扩展。在表中可以保存JSON类型。实际上SQLite将JSON类型的

    postgresql中对jsonb的查询及转换

    postgresql中对jsonb的查询及转换,数据,字段,表数据:需要将strata排除,并且过滤info字段中为{}的数据,将jsonb转换成text,替换“,{,}见SQL:sele

    node express返回json数据给前端

    node express返回json数据给前端,上传,格式,请注意,使用res.json()的格式1.前面不能添加 res.writeHead(200, {‘content-type‘: ‘text/

    params 是什么意思,c#里

    params 是什么意思,c#里,参数,数组,本文目录params 是什么意思,c#里params.add跟params.put有区别吗C#中ref,out和params有什么区别发送请

    C# 基于StackExchange.Redis.dll利用Redis实现分布式Session

    C# 基于StackExchange.Redis.dll利用Redis实现分布式Session,令牌,客户端,最近在研发一款O2O产品,考虑到分布式架构的需要,以前那一套.NET的

    php中json字符串如何转csv格式

    php中json字符串如何转csv格式,文件,字符串,数据,文件句柄,分隔符,字段,php中json字符串转csv格式的方法:1、创建一个php示例文件;2、将JSON字符

    JSONArray是什么

    JSONArray是什么,方法,对象,用于,文本,类型,字符串,JSONArray是一个有序的值序列,它的外部文本形式是一个用方括号括起来的字符串,用逗号分隔值,内

    json数据格式有哪些

    json数据格式有哪些,数据格式,格式,对象,数组,教程,集合,json有两种数据格式,分别是:1、对象格式,“{"key1":obj,"key2":obj,"key3":obj...}

    c#中 (ToolStrip

    c#中 (ToolStrip,窗体,控件,本文目录c#中 (ToolStrip)控件是做什么用的winform窗体问题 toolstrip怎么重用,每个窗体都要用到同样的工具栏窗体

    Json和Lua table互转的Lua模块

    Json和Lua table互转的Lua模块,分词,字符串,先分词,再解析,少写了语法检查module( "json_to_lua", package.seeall )--##################

    C#取得DataTable最大值、最小值

    C#取得DataTable最大值、最小值,最大值,最小值,C#取得DataTable最大值、最小值int max=int.Parse((dtItemsAll.Compute("Max(CPITEMS_SOR