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
}

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ù)的輸出?

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 "";
}
}
}
}
- 3 回答
- 0 關注
- 216 瀏覽
添加回答
舉報