第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何從 JSON 模型訪問值

如何從 JSON 模型訪問值

C#
慕桂英546537 2021-11-07 19:52:33
在此代碼中,我將值設置為來自JSON名為myJson.code 的文件的模型類工作正常,并且值綁定到模型類沒有任何問題。我的代碼如下。class Program{    static void Main(string[] args)    {         ReadJson();    }    public static string ReadJson()    {        string case20 = "this is 20", case40 = "this is 40";        string json;        using (StreamReader r = new StreamReader(@"C:\Users\HP\Desktop\Output\myJson.json"))        {            json = r.ReadToEnd();        }        MyClass response = JsonConvert.DeserializeObject<MyClass>(json);        var dropDowns = response;        string jsonDropdown = JsonConvert.SerializeObject(dropDowns, Formatting.Indented);        return "";    }}這是我的模型類:public class Customer{    public int RATE { get; set; }    public int FEE { get; set; }    public int PERIOD { get; set; }    public string NewValue    {        get        {            var rateVal = this.RATE;            switch (rateVal)            {                case 20:                    return ""; // Here, I need to get value from the ReadJson method to return (value of case20)                case 40:                    return ""; // Here, I need to get value from the ReadJson method to return (value of case40)            }            return "test";        }    }}public class MyClass{    public string StatusCode { get; set; }    public Customer Customer { get; set; }    public object LoanDetail { get; set; }}之后,我設置的值,以模型類,從Customer類我要檢查的RATE,為了的價值RATE我想返回的值case20,并case40在變量值ReadJson()的方法。我如何訪問這些值表單Customer類。提前致謝?。。?
查看完整描述

3 回答

?
千巷貓影

TA貢獻1829條經(jīng)驗 獲得超7個贊

根據(jù)下面更改您的代碼,它會執(zhí)行您想要的操作。但是我仍然無法理解為什么在您已經(jīng)擁有 RATE 屬性的情況下還需要 NewValue 屬性。它們不具有相同的價值嗎?您根據(jù) RATE 設置 NewValue,為什么不在任何地方只使用 RATE?


    public static string ReadJson()

    {

        string json = File.ReadAllText(@"C:\Users\HP\Desktop\Output\myJson.json");


        MyClass response = JsonConvert.DeserializeObject<MyClass>(json);


        return JsonConvert.SerializeObject(response, Formatting.Indented);

    }


public class Customer


{

    public int RATE { get; set; }

    public int FEE { get; set; }

    public int PERIOD { get; set; }


    public string NewValue

    {

        get

        {

            switch (RATE)

            {

                case 20:

                    return "this is 20";

                case 40:

                    return "this is 40";

                default:

                    return "";

            }

        }

    }

}


public static void Main(string[] args)


    {

        Console.WriteLine(ReadJson());

    }

輸出是:


{

  "StatusCode": "100",

  "Customer": {

    "RATE": 20,

    "FEE": 3000,

    "PERIOD": 60,

    "NewValue": "this is 20"

  },

  "LoanDetail": null

}


查看完整回答
反對 回復 2021-11-07
?
長風秋雁

TA貢獻1757條經(jīng)驗 獲得超7個贊

您的函數(shù)正在將字符串中的 JSON 數(shù)據(jù)讀取到您的客戶對象中。

不應該在客戶對象或程序中設置 case20 & case40 嗎?

您沒有從我能看到的任何地方讀取 JSON 中的 case20 或 40 個字符串。所以我假設他們的輸出在運行程序時不會動態(tài)改變。

你也var rateVale = this.rate;應該像var rateVal = int.Parse(this.rate);你將它作為一個整數(shù)進行比較。無論是那個還是 switch case 都應該是“20”而不是 20 等。

您能否包含您擁有的代碼示例以及適當?shù)膶ο笾祽撌鞘裁匆约?newValue 參數(shù)的輸出?


查看完整回答
反對 回復 2021-11-07
?
慕蓋茨4494581

TA貢獻1850條經(jīng)驗 獲得超11個贊

感謝所有幫助我解決這個問題的人。我找到了解決我的問題的方法。


我創(chuàng)建了一個靜態(tài)類,名為TestCase.


public static class TestCase

{

    public static string case20 { get; set; }

    public static string case40 { get; set; }

}

然后將值設置為 ReadJson()


public static string ReadJson()

{

    TestCase.case20 = "this is 20";

    TestCase case40 = "this is 40";

    // same code has beign used....

}

然后在Customer類中,我按如下方式訪問這些值。


public class Customer


{

    public int RATE { get; set; }

    public int FEE { get; set; }

    public int PERIOD { get; set; }


    public string NewValue

    {

        get

        {

            switch (RATE)

            {

                case 20:

                    return TestCase.case20;

                case 40:

                    return TestCase.case40;

                default:

                    return "";

            }

        }

    }

}


查看完整回答
反對 回復 2021-11-07
  • 3 回答
  • 0 關注
  • 216 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號