繁体   English   中英

如何在 C# 的 Winform tabPage 上显示反序列化的 Json 结果

[英]How to display deserialized Json result on Winform tabPage in C#

请我在这里需要你的帮助,我无法编写逻辑来在 Winforms C# 的 tabPage 上显示反序列化的 JSON 结果

    public Form1()
    {
       

        var path = @"C:\Users\elitebook\source\repos\ExamCBT\question.json";

        string jsonFile = File.ReadAllText(path);

        Question q = JsonConvert.DeserializeObject<Question>(jsonFile);

          InitializeComponent();
       
    }

 jsonFile = tabPage2.ToString();

您可以使用这些方法对 JSON 进行序列化和反序列化。 只需下载Newtonsoft.Json NuGet package。

static class FileHelper
{
    public static void JsonSerialize(object obj, string fileName)
    {
        using (var sw = new System.IO.StreamWriter($"{fileName}.json"))
            using (var jw = new Newtonsoft.Json.JsonTextWriter(sw))
            {
                var serializer = new Newtonsoft.Json.JsonSerializer();
                jw.Formatting = Newtonsoft.Json.Formatting.Indented;
                serializer.Serialize(jw, obj);
            }
    }
    public static void JsonDeserialize(out string/*object, List<>, or something else*/ obj, string fileName)
    {
        using (var sr = new System.IO.StreamReader($"{fileName}.json"))
        {
            using (var jr = new Newtonsoft.Json.JsonTextReader(sr))
            {
                var serializer = new Newtonsoft.Json.JsonSerializer();
                try
                {
                    obj = serializer.Deserialize<string /*object, List<>, or something else...*/>(jr);
                }
                catch
                {
                    obj = null;
                }
            }
        }
    }
}

您可以像这样将 JSON 数据添加到TabPage

Label label = new Label(); // label can be textbox, button or something else...
FileHelper.JsonDeserialize(out string /*object, List<>, or something else...*/ data, "test");
label.Text = data //data type is string
tabPage1.Controls.Add(label);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM