我无法对以下回复进行反序列化: 我的JSON是
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,
}
}
控制器是:
[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);
}
答案 0 :(得分:5)
按照此处:http://www.newtonsoft.com/json/help/html/DeserializeObject.htm
不要忘记为反序列化对象声明一个类。它应该包含json对象已经拥有的字段或其中的一些字段。
我不知道你的代码是否完全有效,除了反序列化过程,但下面有一个代码示例让你理解我的意思:
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);
...
}