C#使用Newtonsoft.Json中的JObject對象
更新時間:2022年07月23日 09:34:57 作者:熊思宇
本文詳細(xì)講解了C#使用Newtonsoft.Json中JObject對象的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
案例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)行

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
C#緩存之SqlCacheDependency用法實例總結(jié)
這篇文章主要介紹了C#緩存之SqlCacheDependency用法,在C#程序設(shè)計中有一定的實用價值,需要的朋友可以參考下2014-08-08
C#中控制遠(yuǎn)程計算機(jī)的服務(wù)的方法
C#中控制遠(yuǎn)程計算機(jī)的服務(wù)的方法...2007-04-04
C#使用Spire.XLS for .NET實現(xiàn)Excel和CSV互轉(zhuǎn)的全過程
在現(xiàn)代數(shù)據(jù)處理和分析中,Excel和CSV作為兩種最常見的數(shù)據(jù)格式,扮演著舉足輕重的角色,然而,在實際開發(fā)中,開發(fā)者經(jīng)常面臨c# excel to csv或csv to excel的數(shù)據(jù)轉(zhuǎn)換挑戰(zhàn),本文將深入探討如何利用Spire.XLS for .NET輕松實現(xiàn)Excel和CSV文件之間的雙向轉(zhuǎn)換2025-09-09

