Reputation:
I am trying to convert this response to object so that i can use access them using object in c# mvc application.
Following is the code i did:
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);
}
Following is the Json response but its in string format:
{"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"}}
any help would be highly appreciated> Thanks
Upvotes: 2
Views: 7308
Reputation: 440
Try this:
using Newtonsoft.Json.Linq;
...
var jso = JObject.Parse(content);
string value = jso["propertyname"].Value<string>();
Upvotes: 0
Reputation: 3439
You can utilize Visual Studio's Paste Special feature:
Copy JSON and Paste Special:
You will get following classes:
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; }
}
Now simply utilize Newtonsoft to deserialize:
var items = JsonConvert.DeserializeObject<JsonResponse>(json);
Output:
Upvotes: 9