簡體   English   中英

在 JSON 中取某個字符串並放入 RichTextBox

[英]Take a certain string in JSON and put in RichTextBox

我在 C # Windows 表格中有一個簡單的程序,我需要通過插入 json 來獲取字符串:“htmlMailInfoBytes”}}} 和之前的“htmlMailInfoBytes”:

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        var base64EncodedBytes = System.Convert.FromBase64String(richTextBox1.Text);
        richTextBox2.Text = "" + System.Text.Encoding.ASCII.GetString(base64EncodedBytes);
    }
    catch (Exception errorMsg)
    {
        MessageBox.Show(errorMsg.Message);
    }
}

Newtonsoft.Json package 添加到您的項目中,如@Jack T. Spades 上述評論中所述。 然后您可以進行以下操作以將 JSON 反序列化為您的 object 類型:

string js = "{ \"htmlMailBytes\": \"email @server.com\", \"errorInfo\": \"data\"}"; // Some string for the test
if (JsonConvert.DeserializeObject<ResultObject>(js) is ResultObject result)
{
    richTextBox1.AppendText(Environment.NewLine + result.HtmlMailBytes); 
} 

public partial class ResultObject
{
    [JsonProperty("htmlMailBytes")]
    public string HtmlMailBytes { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM