gpt4 book ai didi

c# - 如何在 C# mvc 中将 Json 结果转换为对象

转载 作者:行者123 更新时间:2023-12-05 08:54:38 25 4
gpt4 key购买 nike

我正在尝试将此响应转换为对象,以便我可以在 C# mvc 应用程序中使用对象访问它们。

以下是我做的代码:

 var response = await client.GetAsync("ApiTest?Amount=" + Amount.ToString() + "&WalletAddress=" + WalletAddress.ToString() + "&TokenCode=" + TokenType.ToString());




if (response.IsSuccessStatusCode)
{

result = response.Content.ReadAsStringAsync().Result;
var test1 = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(result);
(Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(result));

result = await response.Content.ReadAsStringAsync();
var obj =Newtonsoft.Json.JsonConvert.DeserializeObject(result);
return Json(new { Message = "Your Transaction Has Been Completed Successfully!" }, JsonRequestBehavior.AllowGet);

}

以下是 Json 响应,但它是字符串格式:

{"Error":"Transaction amount must be greater than 0","Result":null,"IsSuccess":false,"HttpResponse":{"Headers":[{"Key":"X-Frame-Options","Value":["sameorigin"]},{"Key":"Strict-Transport-Security","Value":["max-age=31536000"]},{"Key":"Pragma","Value":["no-cache"]},{"Key":"Access-Control-Allow-Origin","Value":["*"]},{"Key":"Keep-Alive","Value":["timeout=5, max=100"]},{"Key":"Connection","Value":["Keep-Alive"]},{"Key":"Cache-Control","Value":["no-store, must-revalidate, no-cache, post-check=0, pre-check=0"]},{"Key":"Date","Value":["Wed, 28 Feb 2018 09:43:57 GMT"]},{"Key":"Set-Cookie","Value":["PHPSESSID=3vbjmnpea9i9n871a8knc3s89q7lufpn; path=/; secure; HttpOnly","visid_incap_992349=On7CIEXMQBq9AtX5/PvHQtp5lloAAAAAQUIPAAAAAACXLL2Z399YXaT6IXztsol+; expires=Wed, 27 Feb 2019 14:49:04 GMT; path=/; Domain=.coinpayments.net","incap_ses_478_992349=pCsbJzCRvCFLbgPwODOiBtx5lloAAAAAR8gvl6uEmcAX0kCi3b/2Ig==; path=/; Domain=.coinpayments.net"]},{"Key":"Server","Value":["Apache"]},{"Key":"X-Iinfo","Value":["5-23697956-23698018 NNNN CT(1461 273 0) RT(1519811034346 506) q(0 0 17 1) r(18 19) U6"]},{"Key":"X-CDN","Value":["Incapsula"]}],"ContentBody":"{\"error\":\"Transaction amount must be greater than 0\",\"result\":[]}","StatusCode":200,"IsSuccessStatusCode":true,"RequestUri":"https://www.coinpayments.net/api.php","RequestBody":"cmd=create_transaction\u0026amount=0\u0026currency1=USD\u0026currency2=LTCT\u0026buyer_email=3Pt5ebwZsMWV2ij1bnFv1yJYk2155PGzGx\u0026version=1\u0026key=c84f65f198e77895f3edc08e7654379785f1057c7c0c6115bee69ed68371d558"}}

任何帮助将不胜感激>谢谢

最佳答案

您可以利用 Visual Studio 的选择性粘贴功能:

复制 JSON 并选择性粘贴:

enter image description here

您将获得以下类(class):

public class JsonResponse
{
public string Error { get; set; }
public object Result { get; set; }
public bool IsSuccess { get; set; }
public Httpresponse HttpResponse { get; set; }
}

public class Httpresponse
{
public Header[] Headers { get; set; }
public string ContentBody { get; set; }
public int StatusCode { get; set; }
public bool IsSuccessStatusCode { get; set; }
public string RequestUri { get; set; }
public string RequestBody { get; set; }
}

public class Header
{
public string Key { get; set; }
public string[] Value { get; set; }
}

现在简单地利用 Newtonsoft 反序列化:

var items = JsonConvert.DeserializeObject<JsonResponse>(json);  

输出:

enter image description here

关于c# - 如何在 C# mvc 中将 Json 结果转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49026451/

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