[英]Outputting data from JSON to Textbox C#
我是C#的新手,所以這可能是一個非常愚蠢的問題。 我的程序是將api請求發送到服務器,並將數據輸出到TextBox。 我已經處理了對API的調用,它以JSON格式接收所有信息。
public void button2_Click(object sender, EventArgs e)
{
var OTPSCODE = new TOTP("CODE");
string API = "API KEY";
string REQ;
REQ = SendRequest("WEBSITE"+API+"&code="+OTPSCODE.now());
if (REQ != null)
{
//MessageBox.Show(REQ, "Hey there!", MessageBoxButtons.OK, MessageBoxIcon.Information);
string json = Newtonsoft.Json.JsonConvert.SerializeObject(REQ);
BalanceTB.Text = // This is Where I want the output to be;
}
}
private string SendRequest(string url)
{
try
{
using (WebClient client = new WebClient())
{
return client.DownloadString(new Uri(url));
}
}
catch (WebException ex)
{
MessageBox.Show("Error while receiving data from the server:\n" + ex.Message, "Something broke.. :(", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return null;
}
}
Web API返回以下內容:
{ "status" : "success",
"data" : {
"available_balance" : "0",
"pending_withdrawals" : "0.0000",
"withdrawable_balance" : "0"
}
}
問題是我不知道如何在文本框中僅顯示JSON [“ status”]或JSON [“ withdrawable_balance”]中的數字。 有人能幫我嗎?
您不應該再次序列化json string
,而是要反序列化它:
var request = "WEBSITE"+API+"&code="+OTPSCODE.now();
var json = SendRequest(request);
if (json != null)
{
//MessageBox.Show(REQ, "Hey there!", MessageBoxButtons.OK, MessageBoxIcon.Information);
var response = Newtonsoft.Json.Linq.JObject.Parse(json);
BalanceTB.Text = string.Format("{0} or {1}",
(string)response["status"],
(int)response["data"]["withdrawable_balance"]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.