亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用映射到 C#.net 模型的 Newtonsoft.json 讀取 json 的替代方法

使用映射到 C#.net 模型的 Newtonsoft.json 讀取 json 的替代方法

C#
瀟瀟雨雨 2021-11-14 17:28:54
抱歉沒有為我的主題提供最佳/合適的標題,如果您有比我更好的標題,將很高興更新它。我當前的問題是我必須處理使用 Newtonsoft.JSON 從 .JSON 文件(由 3rd 方源提供)讀取 json 的現有代碼。它也映射到現有的類。我不確定這是否是通過執行 IF-ELSE 來獲得正確的 json 節點的正確方法,也許有人會有其他選擇/比我更好的方法。謝謝你。這不是確切的代碼,但我復制了與我的問題相關的代碼。PET.JSON 文件:(JSON 文件由 3rd 方提供,結構/模式與下面的示例相同,這是他們的格式 - 沒有權限更改 JSON 格式){  "Pet": {    "Dog": {      "cute": true,      "feet": 4    },    "Bird": {      "cute": false,      "feet": 2    }  }}  寵物類和子類(這是第 3 方標準結構,我無權修改)public class Pet{    public Dog dog { get; set; }    public Bird bird { get; set; }    public class Dog {        public bool cute { get; set; }        public int feet { get; set; }    }    public class Bird    {        public bool cute { get; set; }        public int feet { get; set; }    }}  Pet Reader(我需要使用映射到上述模型的這個反序列化 Json 對象,無權修改它們的實現“但”我必須自己管理如何使用 ReadPetJSON() 的返回值)public static Pet ReadPetJSON(){    string JSON_TEXT = File.ReadAllText("PET.JSON");    Pet pet = Newtonsoft.Json.JsonConvert.DeserializeObject<Pet>(JSON_TEXT);    return pet;}  更新:我發現了使用反射,我可以傳遞一個變量名來查找 PropertyName。感謝大家的幫助和投入,我很感激http://mcgivery.com/c-reflection-get-property-value-of-nested-classes/// Using ReflectionPet pet = ReadPetJSON();// Search Bird > feet using 'DOT' delimiterstring searchBy = "bird.feet"foreach (String part in searchBy.Split('.')){    if (pet == null) { return null; }    Type type = pet.GetType();    PropertyInfo info = type.GetProperty(part);    if (info == null) { return null; }  // or make a catch for NullException    pet = info.GetValue(pet, null);}var result = pet.ToString(); // Object result in stringConsole.WriteLine("Bird-Feet: {0}", result);輸出:Bird-Feet: 2
查看完整描述

2 回答

?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

好吧,您可以改為嘗試這些類,然后在 Pet 列表上循環。


  public class JsonParsed

{

    public Dictionary<string, Attribute> Pet { get; set; } 

}


public class Attribute

{

    public bool cute { get; set; }

    public int feet { get; set; }

}

鍵包含寵物的名稱,屬性將包含其他屬性。


  var json = @"{'Pet': {'Dog': {'cute': true,'feet': 4},'Bird': {'cute': 

  false,'feet': 2}}}";

  var obj = JsonConvert.DeserializeObject<JsonParsed>(json);

 foreach (var element in obj.Pet)

 {

    Console.WriteLine(element.Key  + " has " + element.Value.feet);

 }


查看完整回答
反對 回復 2021-11-14
?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

我是這么想的,但我知道這是不可能的。


是的,是的,可以通過以下代碼實現。


你必須Querying像你的 json


string JSON_TEXT = File.ReadAllText("PET.JSON");


JObject jObject = JObject.Parse(JSON_TEXT);


//This is your search term

string search = "Dog";  //or "Bird", or "Lion", or "Tiger", or "Eagle" or anything that are present it your json


bool cute = (bool)jObject["Pet"][search]["cute"];

int feet = (int)jObject["Pet"][search]["feet"];


string temp = $"My {search} is cute = {cute} and feet = {feet} ";


//If you want it in your strongly type then

Dog dog = jObject["Pet"][search].ToObject<Dog>();

Bird bird = jObject["Pet"][search].ToObject<Bird>();

輸出:

http://img1.sycdn.imooc.com//6190d6fb0001864704840140.jpg

在上面的代碼中

[“寵物”] => json 的第 0 個位置級別節點。

[搜索] => json 的第一個位置級別節點。

["cute"] 或 ["feet"] => 你的 json 的第二個位置級別節點,還有更多你的 json 深度

你可以在這里找到更多信息Querying Json


查看完整回答
反對 回復 2021-11-14
  • 2 回答
  • 0 關注
  • 343 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號