我使用.NET Core for Linux作为控制台程序。 使用Http功能,我可以从Web服务获得一些信息。 然后我试图将结果转换为对象,但我无法使用JSON。
我看了this article,但我找不到任何示例而我无法访问JavaScriptSerializer
public async void CallApi(Object stateInfo)
{
var client = new HttpClient();
var requestContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("pair", "XETHZEUR"), });
HttpResponseMessage response = await client.PostAsync("https://api.kraken.com/0/public/Trades", requestContent);
HttpContent responseContent = response.Content;
using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
{
String result = await reader.ReadToEndAsync();
//Here I would like to do a deserialized of my variable result using JSON (JObject obj = (JObject)JsonConvert.DeserializeObject(result);) But I don't find any JSON object
}
}
修改 我想知道如何使用JSON将我的变量结果转换为像c#一样的对象:
JObject obj = (JObject)JsonConvert.DeserializeObject(result);
我希望你能帮助我。
非常感谢,
答案 0 :(得分:0)
您只需要某种可用于.NET核心的依赖项,它可以帮助您反序列化json。
Newtonsoft.Json是defacto标准,在.NET核心中可用,你必须将它添加到project.json文件中
"dependencies" {
...
"Newtonsoft.Json": "10.0.3"
},
你班级中适当的使用陈述
using Newtonsoft.Json
然后您可以使用JsonConvert.DeserializeObject(json);
public async void CallApi(Object stateInfo)
{
var client = new HttpClient();
var requestContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("pair", "XETHZEUR"), });
HttpResponseMessage response = await client.PostAsync("https://api.kraken.com/0/public/Trades", requestContent);
HttpContent responseContent = response.Content;
using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
{
String result = await reader.ReadToEndAsync();
//Here I would like to do a JSON Convert of my variable result
var yourObject = JsonConvert.DeserializeObject(result);
}
}