gpt4 book ai didi

c# - C# JSON 反序列化为字符串的结果

转载 作者:太空宇宙 更新时间:2023-11-03 13:07:19 26 4
gpt4 key购买 nike

我使用 json2csharp 来生成函数和类,但我是一个该死的新手。我想要的是使用 JSON 数组中的数据并将其显示在文本框中。

代码如下:

 public class Sent_SMS
{
public string status { get; set; }
public string error { get; set; }
public string smslog_id { get; set; }
public string queue { get; set; }
public string to { get; set; }
}

public class RootObject
{
public List<Sent_SMS> data { get; set; }
public object error_string { get; set; }
public int timestamp { get; set; }
}

public void doSendSMS()
{
/* API URLs */

APIURL_Send = "http://ipadressofgateway/playsms/index.php?app=ws&op=pv&h=" + apikey + "&u=" + username + "&to=" + receiver_number + "&msg=" + message; // Sending Message



using (WebClient wc = new WebClient())
{
var json = wc.DownloadString(APIURL_Send);
var SMS_Log = JsonConvert.DeserializeObject<RootObject>(json);
richTextBox3.Text = "SMS has been sent to:" + SMS_Log.data.to + "Status is:" + SMS_Log.data.status;
}

}

但是当然.. 这不起作用,因为“SMS_Log.data.to”和“SMS_Log.data.status”不正确。如何正确执行此操作?

问候

最佳答案

如果您确定响应中始终只有一条短信,则将您的代码更改为:

richTextBox3.Text = "SMS has been sent to:" + SMS_Log.data[0].to + "Status is:" + SMS_Log.data[0].status;

否则,我会寻求这样的解决方案:

var text = "";
foreach (var sms in SMS_Log.data) {
text += "SMS has been sent to:" + sms.to + "Status is:" + sms.status + "\n";
}
richTextBox3.Text = text;

关于c# - C# JSON 反序列化为字符串的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30325071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com