当前位置:网站首页>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
边栏推荐
- 1286_FreeRTOS的任务优先级设置实现分析
- Router 6/ 以及和Router5 的区别
- 【Tikhonov】基于Tikhonov正则化的图像超分辨率重建
- MySQL table partition creation method
- How to choose a product manager course when changing to a product manager?
- 树莓派4的WiFi设置
- ctfshow-web354(SSRF)
- C语言实现【扫雷游戏】完整版(实现源码)
- Introduction to spark (one article is enough)
- 了解ESP32睡眠模式及其功耗
猜你喜欢

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

Why are so many people turning to product managers? What is the development prospect of product manager?
![C language implementation [minesweeping game] full version (implementation source code)](/img/70/60f9a61bd99fa5fb5fab679a32528e.png)
C language implementation [minesweeping game] full version (implementation source code)
![[network planning] (I) hub, bridge, switch, router and other concepts](/img/7b/fcef37496517c854ac1dbfb35fa3f4.png)
[network planning] (I) hub, bridge, switch, router and other concepts

了解ESP32睡眠模式及其功耗

Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)

未来互联网人才还稀缺吗?哪些技术方向热门?

盘点华为云GaussDB(for Redis)六大秒级能力

Esp32 esp-idf ADC monitors battery voltage (with correction)

ESP32 - ULP 协处理器在低功耗模式下读片内霍尔传感器HALL SENSOR
随机推荐
C语言实现【扫雷游戏】完整版(实现源码)
[classification model] Q-type cluster analysis
【分类模型】Q 型聚类分析
Spark入门(一篇就够了)
C language implementation [Sanzi chess game] (step analysis and implementation source code)
rclone 访问web界面
【推荐技术】基于协同过滤的网络信息推荐技术matlab仿真
Code practice - build your own diffusion models / score based generic models from scratch
ESP32 ESP-IDF ADC监测电池电压(带校正)
ctfhub-端口扫描(SSRF)
Programming examples of stm32f1 and stm32subeide infrared receiving and decoding of NEC protocol
【图像处理】图像直方图均衡化系统含GUI界面
【LINGO】求七个城市最小连线图,使天然气管道价格最低
[FPGA frame difference] FPGA implementation of frame difference target tracking based on vmodcam camera
[lingo] solve quadratic programming
1286_FreeRTOS的任务优先级设置实现分析
How to use SCI hub
SQL language learning record I
女生适合学产品经理吗?有什么优势?
运维面临挑战?智能运维管理系统来帮您