以下是使用RestSharp检索Freebase主题的简单示例:
using System.Collections.Generic;
using RestSharp;
using RestSharp.Deserializers;
namespace Freebase
{
public class FreebaseTopic
{
public string id { get; set; }
public Dictionary<string, Dictionary<string, object>> property { get; set; }
}
class Program
{
static void Main(string[] args)
{
var restClient = new RestClient("https://www.googleapis.com");
var restRequest = new RestRequest("freebase/v1/topic/m/0jdh5");
var response = restClient.Execute(restRequest);
var jsonDeserializer = new JsonDeserializer();
var result = jsonDeserializer.Deserialize<FreebaseTopic>(response);
}
}
}
该示例有效。这是result
变量的一部分:
以下示例:
using System.Collections.Generic;
using RestSharp;
using RestSharp.Deserializers;
namespace Freebase
{
public class ValueSet
{
public string valuetype;
public List<Dictionary<string, object>> values;
public decimal count;
}
public class FreebaseTopic
{
public string id { get; set; }
public Dictionary<string, ValueSet> property { get; set; }
}
class Program
{
static void Main(string[] args)
{
var restClient = new RestClient("https://www.googleapis.com");
var restRequest = new RestRequest("freebase/v1/topic/m/0jdh5");
var response = restClient.Execute(restRequest);
var jsonDeserializer = new JsonDeserializer();
var result = jsonDeserializer.Deserialize<FreebaseTopic>(response);
}
}
}
对于其成员,<{1}},ValueSet
和null
始终生成null
个对象:
我更喜欢靠近第二个例子的模型。有没有办法让这样的东西正确地反序列化?
以下是0
:
response
答案 0 :(得分:2)
这似乎是来自RestSharp dll的json反序列化器的内部反序列化问题。 使用Json.NET,这非常有效:
答案 1 :(得分:0)
以下是基于Yuval答案的完整程序:
using System.Collections.Generic;
using RestSharp;
using RestSharp.Deserializers;
using Newtonsoft.Json;
namespace Freebase
{
public class ValueSet
{
public string valuetype;
public List<Dictionary<string, object>> values;
public double count;
}
public class FreebaseTopic
{
public string id { get; set; }
public Dictionary<string, ValueSet> property { get; set; }
}
class Program
{
static void Main(string[] args)
{
var restClient = new RestClient("https://www.googleapis.com");
var restRequest = new RestRequest("freebase/v1/topic/m/0jdh5");
var response = restClient.Execute(restRequest);
var result = JsonConvert.DeserializeObject<FreebaseTopic>(response.Content);
}
}
}