Reputation: 11
I am not able to deserialize following response : My JSON IS
json is
{
"disclaimer": "Exchange rates/",
"license": "Data sourced from various providers",
"timestamp": 1435813262,
"base": "USD",
"rates": {
"AED": 3.672973,
"AFN": 60.150001,
"ALL": 126.7792,
"AMD": 472.46,
"ANG": 1.78875,
"AOA": 121.253666,
"ARS": 9.095239,
"AUD": 1.307011,
"AWG": 1.793333,
"AZN": 1.04955,
}
}
Controller is :
[HttpPost]
public ActionResult Index(Test1 values)
{
string appid = values.apikey;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://openexchangerates.org//api/latest.json?app_id=5db2fa81c8174a839756eb4d5a4a5e05");
request.Method = "POST";
using (StreamWriter streamWriter = new StreamWriter(request.GetRequestStream(), ASCIIEncoding.ASCII))
{
streamWriter.Write(appid1);
streamWriter.Close();
}
string responseText = String.Empty;
if (request.Headers.Count > 0)
{
using (StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream()))
{
responseText = sr.ReadToEnd();
}
}
var myObject = JsonConvert.DeserializeObject(responseText);
}
Upvotes: 0
Views: 1769
Reputation: 415
Follow here : http://www.newtonsoft.com/json/help/html/DeserializeObject.htm
Do not forget to declare a class for your deserialized object. It should contains fields that json object already has or some of them.
I dont know if your code fully works except deserializing process but there is a code example below for you to make you understand what i meant:
class MyClass
{
public string disclaimer { get; set; }
public string license { get; set; }
public string timestamp { get; set; }
...
}
[HttpPost]
public ActionResult Index(Test1 values)
{
...
var myObject = JsonConvert.DeserializeObject<MyClass>(responseText);
...
}
Upvotes: 5