当前位置:网站首页>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
边栏推荐
- C language implementation [Sanzi chess game] (step analysis and implementation source code)
- 盘点华为云GaussDB(for Redis)六大秒级能力
- Dirty reading, unreal reading and unrepeatable reading
- 手机开户选哪个证券公司比较好,哪个更安全
- ctfshow-web351(SSRF)
- go-etcd
- How to use SCI hub
- Is it suitable for girls to study product manager? What are the advantages?
- rclone配置minio及基本操作
- 【分类模型】Q 型聚类分析
猜你喜欢
关于图灵测试和中文屋Chinese room的理解
ESP32 ESP-IDF ADC监测电池电压(带校正)
JSP - 分页
Solve the problem of "unexpected status code 503 service unavailable" when kaniko pushes the image to harbor
Jena基于OWL的默认推理查询
Problem: officeexception: failed to start and connect (II)
[matlab] solve nonlinear programming
【LINGO】求七个城市最小连线图,使天然气管道价格最低
Understand esp32 sleep mode and its power consumption
[FPGA frame difference] FPGA implementation of frame difference target tracking based on vmodcam camera
随机推荐
图解事件坐标screenX、clientX、pageX, offsetX的区别
We found a huge hole in MySQL: do not judge the number of rows affected by update!!!
ctfshow-web354(SSRF)
Dirty reading, unreal reading and unrepeatable reading
Reply and explanation on issues related to "online training of network security education in 2022"
Is it safe to do fund fixed investment on Great Wall Securities?
ESP32 - ULP 协处理器在低功耗模式下读片内霍尔传感器HALL SENSOR
女生适合学产品经理吗?有什么优势?
rclone中文文档:常用命令大全
TDB中多个model情况下使用fuseki查询
在长城证券上做基金定投安全吗?
How to use Alibaba vector font files through CDN
Solve the problem that the class defined in meta-inf.services cannot be read
Esp32 esp-idf ADC monitors battery voltage (with correction)
Rclone Chinese document: a collection of common commands
K8s set up redis cluster
[lingo] solve quadratic programming
灰度何以跌下神坛?
ctfshow-web355,356(SSRF)
电脑有网络,但所有浏览器网页都打不开,是怎么回事?