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

时间:2018-02-28 09:48:13

标签: c# json asp.net-mvc json.net

我正在尝试将此响应转换为对象,以便我可以使用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响应,但是采用字符串格式:

  

{“错误”:“交易金额必须大于   0" , “结果”:NULL, “IsSuccess”:假 “的HttpResponse”:{ “头”:[{ “密钥”: “X框-选项”, “值”:[ “SAMEORIGIN”]},{ “关键”:“严,运输和安全”,“值”:“最大年龄= 31536000”]},{“关键”:“语法”,“值”:“无缓存”]},{ “Key”:“Access-Control-Allow-Origin”,“Value”:[“*”]},{“Key”:“Keep-Alive”,“Value”:[“timeout = 5,max = 100” ]},{ “密钥”: “连接”, “值”:[ “保持活动”]},{ “键”: “缓存控制”, “值”:[“无商店,   must-revalidate,no-cache,post-check = 0,   pre-check = 0“]},{”Key“:”Date“,”Value“:[”Wed“,2018年2月28日09:43:57   GMT “]},{” 密钥 “:” 设置Cookie “ ”值“:[” PHPSESSID = 3vbjmnpea9i9n871a8knc3s89q7lufpn;   路径= /;安全;   仅Http “” visid_incap_992349 = On7CIEXMQBq9AtX5 / PvHQtp5lloAAAAAQUIPAAAAAACXLL2Z399YXaT6IXztsol +;   到期日= 2019年2月27日星期三格林尼治标准时间14:49:04;路径= /;   域= .coinpayments.net “” incap_ses_478_992349 = pCsbJzCRvCFLbgPwODOiBtx5lloAAAAAR8gvl6uEmcAX0kCi3b / 2Ig ==;   路径= /;   域= .coinpayments.net “]},{” 密钥 “:” 服务器 “ ”值“:[ ”阿帕奇“]},{ ”键“: ”X-Iinfo“, ”值“:[” 5-23697956 -23698018   NNNN CT(1461 273 0)RT(1519811034346 506)q(0 0 17 1)r(18 19)   U6 “]},{” 键 “:” X-CDN”, “值”:[ “Incapsula”]}], “ContentBody”: “{\” 错误\ “:\” 交易   金额必须大于   0 \ “\ ”结果\“:[]}”, “的StatusCode”:200, “IsSuccessStatusCode”:真 “RequestUri”: “https://www.coinpayments.net/api.php”, “RequestBody”:“CMD = create_transaction \ u0026amount = 0 \ u0026currency1 = USD \ u0026currency2 = LTCT \ u0026buyer_email = 3Pt5ebwZsMWV2ij1bnFv1yJYk2155PGzGx \ u0026version = 1 \ u0026key = c84f65f198e77895f3edc08e7654379785f1057c7c0c6115bee69ed68371d558" }}

任何帮助都将受到高度赞赏&gt; 感谢

2 个答案:

答案 0 :(得分:6)

您可以使用Visual Studio的粘贴特殊功能:

复制JSON和Paste Special:

enter image description here

您将获得以下课程:

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

答案 1 :(得分:0)

试试这个:

using Newtonsoft.Json.Linq;
...
var jso = JObject.Parse(content);
string value = jso["propertyname"].Value<string>();