猿问

如何从 JSON 模型访问值

在此代码中,我将值设置为来自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类。


提前致谢!!!


慕桂英546537
浏览 172回答 3
3回答

千巷猫影

根据下面更改您的代码,它会执行您想要的操作。但是我仍然无法理解为什么在您已经拥有 RATE 属性的情况下还需要 NewValue 属性。它们不具有相同的价值吗?您根据 RATE 设置 NewValue,为什么不在任何地方只使用 RATE?&nbsp; &nbsp; public static string ReadJson()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string json = File.ReadAllText(@"C:\Users\HP\Desktop\Output\myJson.json");&nbsp; &nbsp; &nbsp; &nbsp; MyClass response = JsonConvert.DeserializeObject<MyClass>(json);&nbsp; &nbsp; &nbsp; &nbsp; return JsonConvert.SerializeObject(response, Formatting.Indented);&nbsp; &nbsp; }public class Customer{&nbsp; &nbsp; public int RATE { get; set; }&nbsp; &nbsp; public int FEE { get; set; }&nbsp; &nbsp; public int PERIOD { get; set; }&nbsp; &nbsp; public string NewValue&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (RATE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 20:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "this is 20";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 40:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "this is 40";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}public static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(ReadJson());&nbsp; &nbsp; }输出是:{&nbsp; "StatusCode": "100",&nbsp; "Customer": {&nbsp; &nbsp; "RATE": 20,&nbsp; &nbsp; "FEE": 3000,&nbsp; &nbsp; "PERIOD": 60,&nbsp; &nbsp; "NewValue": "this is 20"&nbsp; },&nbsp; "LoanDetail": null}

长风秋雁

您的函数正在将字符串中的 JSON 数据读取到您的客户对象中。不应该在客户对象或程序中设置 case20 & case40 吗?您没有从我能看到的任何地方读取 JSON 中的 case20 或 40 个字符串。所以我假设他们的输出在运行程序时不会动态改变。你也var rateVale = this.rate;应该像var rateVal = int.Parse(this.rate);你将它作为一个整数进行比较。无论是那个还是 switch case 都应该是“20”而不是 20 等。您能否包含您拥有的代码示例以及适当的对象值应该是什么以及 newValue 参数的输出?

慕盖茨4494581

感谢所有帮助我解决这个问题的人。我找到了解决我的问题的方法。我创建了一个静态类,名为TestCase.public static class TestCase{&nbsp; &nbsp; public static string case20 { get; set; }&nbsp; &nbsp; public static string case40 { get; set; }}然后将值设置为 ReadJson()public static string ReadJson(){&nbsp; &nbsp; TestCase.case20 = "this is 20";&nbsp; &nbsp; TestCase case40 = "this is 40";&nbsp; &nbsp; // same code has beign used....}然后在Customer类中,我按如下方式访问这些值。public class Customer{&nbsp; &nbsp; public int RATE { get; set; }&nbsp; &nbsp; public int FEE { get; set; }&nbsp; &nbsp; public int PERIOD { get; set; }&nbsp; &nbsp; public string NewValue&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (RATE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 20:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return TestCase.case20;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 40:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return TestCase.case40;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答