C#使用Newtonsoft.Json中的JObject對(duì)象
案例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();
? ? ? ? }
? ? }
}運(yùn)行

案例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();
? ? ? ? }
? ? }
}運(yùn)行

案例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();
? ? ? ? }
? ? }
}運(yùn)行

案例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();
? ? ? ? }
? ? }
}運(yùn)行

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- C#下Newtonsoft.Json的具體使用
- C# Newtonsoft.Json庫(kù)的常用屬性和方法詳解
- C#使用Newtonsoft.Json庫(kù)實(shí)現(xiàn)JSON數(shù)據(jù)中某個(gè)字段值的提取功能
- C# Newtonsoft.Json用法詳解
- C# newtonsoft.json中文亂碼問號(hào)的解決方案
- c# Newtonsoft.Json 常用方法總結(jié)
- C# Newtonsoft.Json 解析多嵌套json 進(jìn)行反序列化的實(shí)例
- c#添加Newtonsoft.Json包的操作
- C# Newtonsoft.Json 的使用說明
- C#中Newtonsoft.Json 到 System.Text.Json 遷移避坑指南
相關(guān)文章
C#對(duì)Access進(jìn)行增刪改查的完整示例
本文主要是講C#對(duì)Access數(shù)據(jù)庫(kù)的增刪改查操作,想學(xué)習(xí)C#和Access數(shù)據(jù)庫(kù)操作基礎(chǔ)的可以參考借鑒,以下代碼都經(jīng)過實(shí)踐測(cè)試可用,下面跟著小編一起來(lái)看看。2016-08-08
C#判斷一個(gè)矩陣是否為對(duì)稱矩陣及反稱矩陣的方法
這篇文章主要介紹了C#判斷一個(gè)矩陣是否為對(duì)稱矩陣及反稱矩陣的方法,涉及C#矩陣遍歷及檢查等相關(guān)運(yùn)算技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
C#讀取txt文件數(shù)據(jù)的方法實(shí)例
讀取txt文本數(shù)據(jù)的內(nèi)容,是我們開發(fā)中經(jīng)常會(huì)遇到的一個(gè)功能,這篇文章主要給大家介紹了關(guān)于C#讀取txt文件數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2021-05-05
C#實(shí)現(xiàn)簡(jiǎn)單串口通信的示例詳解
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)串口通信的相關(guān)知識(shí),文中示例代碼介紹的非常詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴們可以跟隨小編一起了解一下2023-10-10
C#使用Shader實(shí)現(xiàn)夜幕降臨倒計(jì)時(shí)的效果
這篇文章主要介紹了C#使用Shader實(shí)現(xiàn)夜幕降臨倒計(jì)時(shí)的效果,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10
C#中后臺(tái)post請(qǐng)求常用的兩種方式總結(jié)
這篇文章主要介紹了C#中后臺(tái)post請(qǐng)求常用的兩種方式總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

