使用RestSharp检索Freebase主题

时间:2014-12-14 03:53:15

标签: c# .net json.net restsharp freebase

以下是使用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变量的一部分:

enter image description here

以下示例:

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}},ValueSetnull始终生成null个对象:

enter image description here

我更喜欢靠近第二个例子的模型。有没有办法让这样的东西正确地反序列化?

以下是0

的一部分
response

2 个答案:

答案 0 :(得分:2)

这似乎是来自RestSharp dll的json反序列化器的内部反序列化问题。 使用Json.NET,这非常有效:

Deserialization using Json.NET Working properly

答案 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);

        }
    }
}