当前位置:网站首页>C# Newtonsoft.Json中JObject的使用
C# Newtonsoft.Json中JObject的使用
2022-07-01 07:01:00 【熊思宇】
目录
案例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();
}
}
}
运行

end
边栏推荐
- ESP32 ESP-IDF ADC监测电池电压(带校正)
- Docker installation and deployment redis
- 问题解决:OfficeException: failed to start and connect(一)
- C# 读写自定义的Config文件
- Solution to the problem that objects in unity2021 scene view cannot be directly selected
- Automated test platform (13): interface automation framework and platform comparison, application scenario analysis and design ideas sharing
- DC-4 target
- K8S搭建Redis集群
- Product learning (I) - structure diagram
- SQL learning notes nine connections 2
猜你喜欢

(I) apple has open source, but so what?

如何制作专属的VS Code主题

C语言实现【扫雷游戏】完整版(实现源码)

比赛即实战!中国软件杯发布全新产业创新赛项,校企可联合参赛

ctfshow-web355,356(SSRF)
![C language implementation [minesweeping game] full version (implementation source code)](/img/70/60f9a61bd99fa5fb5fab679a32528e.png)
C language implementation [minesweeping game] full version (implementation source code)
![[Electrical dielectric number] electrical dielectric number and calculation considering HVDC and facts components](/img/7c/2b1d4797f367cced51f36e8a1bb199.png)
[Electrical dielectric number] electrical dielectric number and calculation considering HVDC and facts components

為什麼這麼多人轉行產品經理?產品經理發展前景如何?

如何画产品架构图?

Figure out the difference between event coordinates screenx, clientx, pagex and offsetx
随机推荐
用手机在指南针上开户靠谱吗?这样有没有什么安全隐患
Is it safe to do fund fixed investment on Great Wall Securities?
Is the account opening of GF Securities safe and reliable? How to open GF Securities Account
记一次线上接口慢查询问题排查
如何通过cdn方式使用阿里巴巴矢量图字体文件
[classification model] Q-type cluster analysis
go-etcd
MySQL data type learning notes
【推荐技术】基于协同过滤的网络信息推荐技术matlab仿真
【电气介数】电气介数及考虑HVDC和FACTS元件的电气介数计算
Esp32 esp-idf ADC monitors battery voltage (with correction)
图解事件坐标screenX、clientX、pageX, offsetX的区别
buildroot override 机制
【分类模型】Q 型聚类分析
Code practice - build your own diffusion models / score based generic models from scratch
Servlet 和 JSP 中的分页
电脑有网络,但所有浏览器网页都打不开,是怎么回事?
问题解决:OfficeException: failed to start and connect(一)
K8s set up redis cluster
解决无法读取META-INF.services里面定义的类