【问题标题】:Retrieving Freebase topics with RestSharp使用 RestSharp 检索 Freebase 主题
【发布时间】:2014-12-14 03:53:14
【问题描述】:

这是一个使用 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);
        }
    }
}

总是为其成员生成带有nullnull0ValueSet 对象:

我更喜欢更接近第二个示例的模型。有没有办法让类似的东西正确反序列化?

这里是response的一部分:

{
 "id": "/m/0jdh5",
 "property": {
  "/award/award_nominated_work/award_nominations": {
   "valuetype": "compound",
   "values": [
    {
     "text": "Nebula Award for Best Novel - Frank Herbert - 1966 Nebula Awards - 1965 - jeff - Award Nomination",
     "lang": "en",
     "id": "/m/043zdj9",
     "creator": "/user/jeff",
     "timestamp": "2008-05-22T00:09:05.002Z",
     "property": {
      "/award/award_nomination/award": {
       "valuetype": "object",
       "values": [
        {
         "text": "Nebula Award for Best Novel",
         "lang": "en",
         "id": "/m/0265vt",
         "creator": "/user/jeff",
         "timestamp": "2008-05-22T00:09:05.002Z"
        }
       ],
       "count": 1.0
      },
      "/award/award_nomination/award_nominee": {
       "valuetype": "object",
       "values": [
        {
         "text": "Frank Herbert",
         "lang": "en",
         "id": "/m/02xyl",
         "creator": "/user/jeff",
         "timestamp": "2008-05-22T00:09:05.002Z"
        }
       ],
       "count": 1.0
      },
      "/award/award_nomination/ceremony": {
       "valuetype": "object",
       "values": [
        {
         "text": "1966 Nebula Awards",
         "lang": "en",
         "id": "/m/0zd3pyf",
         "creator": "/user/schema_bot",
         "timestamp": "2013-12-19T21:32:06.000Z"
        }
       ],
       "count": 1.0
      },
      "/award/award_nomination/year": {
       "valuetype": "datetime",
       "values": [
        {
         "text": "1965",
         "lang": "",
         "value": "1965",
         "creator": "/user/jeff",
         "timestamp": "2008-05-22T00:07:56.000Z"
        }
       ],
       "count": 1.0
      },
      "/type/object/attribution": {
       "valuetype": "object",
       "values": [
        {
         "text": "jeff",
         "lang": "en",
         "id": "/m/0jsf_k",
         "creator": "/user/jeff",
         "timestamp": "2008-05-22T00:07:56.000Z"
        }
       ],
       "count": 1.0
      },
      "/type/object/type": {
       "valuetype": "object",
       "values": [
        {
         "text": "Award Nomination",
         "lang": "en",
         "id": "/award/award_nomination",
         "creator": "/user/jeff",
         "timestamp": "2008-05-22T00:07:56.000Z"
        }
       ],
       "count": 1.0
      }
     }
    },
    {
     "text": "Hugo Award for Best Novel - Frank Herbert - 1964 Hugo Awards - 1964 - earlye - Award Nomination",
     "lang": "en",
     "id": "/m/08f78v2",
     "creator": "/user/earlye",
     "timestamp": "2009-11-15T07:33:55.000Z",
     "property": {
      "/award/award_nomination/award": {
       "valuetype": "object",
       "values": [
        {
         "text": "Hugo Award for Best Novel",
         "lang": "en",
         "id": "/m/01yz0x",
         "creator": "/user/earlye",
         "timestamp": "2009-11-15T07:33:55.000Z"
        }
       ],
       "count": 1.0
      },
      "/award/award_nomination/award_nominee": {
       "valuetype": "object",
       "values": [
        {
         "text": "Frank Herbert",
         "lang": "en",
         "id": "/m/02xyl",
         "creator": "/user/earlye",
         "timestamp": "2009-11-15T07:33:55.000Z"
        }
       ],
       "count": 1.0
      },
      "/award/award_nomination/ceremony": {
       "valuetype": "object",
       "values": [
        {
         "text": "1964 Hugo Awards",
         "lang": "en",
         "id": "/m/0zd5b3c",
         "creator": "/user/schema_bot",
         "timestamp": "2013-12-19T18:54:50.000Z"
        }
       ],
       "count": 1.0
      },
      "/award/award_nomination/year": {
       "valuetype": "datetime",
       "values": [
        {
         "text": "1964",
         "lang": "",
         "value": "1964",
         "creator": "/user/earlye",
         "timestamp": "2009-11-15T07:33:55.000Z"
        }
       ],
       "count": 1.0
      },
      "/type/object/attribution": {
       "valuetype": "object",
       "values": [
        {
         "text": "earlye",
         "lang": "en",
         "id": "/m/08f784x",
         "creator": "/user/earlye",
         "timestamp": "2009-11-15T07:33:55.000Z"
        }
       ],
       "count": 1.0
      },
      "/type/object/type": {
       "valuetype": "object",
       "values": [
        {
         "text": "Award Nomination",
         "lang": "en",
         "id": "/award/award_nomination",
         "creator": "/user/earlye",
         "timestamp": "2009-11-15T07:33:55.000Z"
        }
       ],
       "count": 1.0
      }
     }
    },
    {
     "text": "Hugo Award for Best Novel - Frank Herbert - 1966 Hugo Awards - 1966 - earlye - Award Nomination",
     "lang": "en",
     "id": "/m/08f78vb",
     "creator": "/user/earlye",
     "timestamp": "2009-11-15T07:33:55.001Z",
     "property": {
      "/award/award_nomination/award": {
       "valuetype": "object",
       "values": [
        {
         "text": "Hugo Award for Best Novel",
         "lang": "en",
         "id": "/m/01yz0x",
         "creator": "/user/earlye",
         "timestamp": "2009-11-15T07:33:55.001Z"
        }
       ],
       "count": 1.0
      },
      "/award/award_nomination/award_nominee": {
       "valuetype": "object",
       "values": [
        {
         "text": "Frank Herbert",
         "lang": "en",
         "id": "/m/02xyl",
         "creator": "/user/earlye",
         "timestamp": "2009-11-15T07:33:55.001Z"
        }
       ],
       "count": 1.0
      },
      "/award/award_nomination/ceremony": {
       "valuetype": "object",
       "values": [
        {
         "text": "1966 Hugo Awards",
         "lang": "en",
         "id": "/m/0zd5l6v",
         "creator": "/user/schema_bot",
         "timestamp": "2013-12-19T18:54:07.000Z"
        }
       ],
       "count": 1.0
      },
      "/award/award_nomination/year": {
       "valuetype": "datetime",
       "values": [
        {
         "text": "1966",
         "lang": "",
         "value": "1966",
         "creator": "/user/earlye",
         "timestamp": "2009-11-15T07:33:55.001Z"
        }
       ],
       "count": 1.0
      },
      "/type/object/attribution": {
       "valuetype": "object",
       "values": [
        {
         "text": "earlye",
         "lang": "en",
         "id": "/m/08f784x",
         "creator": "/user/earlye",
         "timestamp": "2009-11-15T07:33:55.001Z"
        }
       ],
       "count": 1.0
      },
      "/type/object/type": {
       "valuetype": "object",
       "values": [
        {
         "text": "Award Nomination",
         "lang": "en",
         "id": "/award/award_nomination",
         "creator": "/user/earlye",
         "timestamp": "2009-11-15T07:33:55.001Z"
        }
       ],
       "count": 1.0
      }
     }
    }
   ],
   "count": 3.0
  },
  "/award/award_winning_work/awards_won": {
   "valuetype": "compound",
   "values": [
    {
     "text": "Hugo Award for Best Novel - Frank Herbert - 1966 Hugo Awards - 1966 - Freebase Data Team - Award Honor",
     "lang": "en",
     "id": "/m/03mlmsd",
     "creator": "/user/gardening_bot",
     "timestamp": "2008-02-18T23:43:14.004Z",
     "property": {
      "/award/award_honor/award": {
       "valuetype": "object",
       "values": [
        {
         "text": "Hugo Award for Best Novel",
         "lang": "en",
         "id": "/m/01yz0x",
         "creator": "/user/gardening_bot",
         "timestamp": "2008-02-18T23:43:14.004Z"
        }
       ],
       "count": 1.0
      },
      "/award/award_honor/award_winner": {
       "valuetype": "object",
       "values": [
        {
         "text": "Frank Herbert",
         "lang": "en",
         "id": "/m/02xyl",
         "creator": "/user/jeff",
         "timestamp": "2008-03-20T22:04:32.000Z"
        }
       ],
       "count": 1.0
      },
      "/award/award_honor/ceremony": {
       "valuetype": "object",
       "values": [
        {
         "text": "1966 Hugo Awards",
         "lang": "en",
         "id": "/m/0zd5l6v",
         "creator": "/user/annawantuch",
         "timestamp": "2013-11-29T21:02:51.000Z"
        }
       ],
       "count": 1.0
      },
      "/award/award_honor/year": {
       "valuetype": "datetime",
       "values": [
        {
         "text": "1966",
         "lang": "",
         "value": "1966",
         "creator": "/user/gardening_bot",
         "timestamp": "2008-02-18T23:43:14.004Z"
        }
       ],
       "count": 1.0
      },
      "/type/object/attribution": {
       "valuetype": "object",
       "values": [
        {
         "text": "Freebase Data Team",
         "lang": "en",
         "id": "/m/03jpr9s",
         "creator": "/user/gardening_bot",
         "timestamp": "2008-02-18T23:43:14.003Z"
        }
       ],
       "count": 1.0
      },
      "/type/object/type": {
       "valuetype": "object",
       "values": [
        {
         "text": "Award Honor",
         "lang": "en",
         "id": "/award/award_honor",
         "creator": "/user/gardening_bot",
         "timestamp": "2008-02-18T23:43:14.004Z"
        }
       ],
       "count": 1.0
      }
     }
    },
    {
     "text": "Nebula Award for Best Novel - Frank Herbert - 1966 Nebula Awards - 1965 - tadhg - Award Honor",
     "lang": "en",
     "id": "/m/040wf_0",
     "creator": "/user/tadhg",
     "timestamp": "2008-04-28T23:08:57.001Z",
     "property": {
      "/award/award_honor/award": {
       "valuetype": "object",
       "values": [
        {
         "text": "Nebula Award for Best Novel",
         "lang": "en",
         "id": "/m/0265vt",
         "creator": "/user/tadhg",
         "timestamp": "2008-04-28T23:08:56.003Z"
        }
       ],
       "count": 1.0
      },
      "/award/award_honor/award_winner": {
       "valuetype": "object",
       "values": [
        {
         "text": "Frank Herbert",
         "lang": "en",
         "id": "/m/02xyl",
         "creator": "/user/tadhg",
         "timestamp": "2008-04-28T23:08:59.000Z"
        }
       ],
       "count": 1.0
      },
      "/award/award_honor/ceremony": {
       "valuetype": "object",
       "values": [
        {
         "text": "1966 Nebula Awards",
         "lang": "en",
         "id": "/m/0zd3pyf",
         "creator": "/user/pkarwat",
         "timestamp": "2013-11-29T13:14:09.000Z"
        }
       ],
       "count": 1.0
      },
      "/award/award_honor/year": {
       "valuetype": "datetime",
       "values": [
        {
         "text": "1965",
         "lang": "",
         "value": "1965",
         "creator": "/user/tadhg",
         "timestamp": "2008-04-28T23:08:57.001Z"
        }
       ],
       "count": 1.0
      },
      "/type/object/attribution": {
       "valuetype": "object",
       "values": [
        {
         "text": "tadhg",
         "lang": "en",
         "id": "/m/01xrv19",
         "creator": "/user/tadhg",
         "timestamp": "2008-04-28T23:08:56.003Z"
        }
       ],
       "count": 1.0
      },
      "/type/object/type": {
       "valuetype": "object",
       "values": [
        {
         "text": "Award Honor",
         "lang": "en",
         "id": "/award/award_honor",
         "creator": "/user/tadhg",
         "timestamp": "2008-04-28T23:08:56.003Z"
        }
       ],
       "count": 1.0
      }
     }
    }
   ],
   "count": 2.0
  },
  "/book/book/characters": {
   "valuetype": "object",
   "values": [
    {
     "text": "Glossu Rabban",
     "lang": "en",
     "id": "/m/03pg1d",
     "creator": "/user/froderik",
     "timestamp": "2007-06-17T13:22:42.000Z"
    },
    {
     "text": "Hasimir Fenring",
     "lang": "en",
     "id": "/m/038wlp",
     "creator": "/user/froderik",
     "timestamp": "2007-06-17T13:21:07.000Z"
    },
    {
     "text": "Princess Irulan",
     "lang": "en",
     "id": "/m/036bx5",
     "creator": "/user/froderik",
     "timestamp": "2007-06-11T14:02:51.000Z"
    },
    {
     "text": "Liet-Kynes",
     "lang": "en",
     "id": "/m/03pg0p",
     "creator": "/user/froderik",
     "timestamp": "2007-06-07T19:42:52.000Z"
    },
    {
     "text": "Chani",
     "lang": "en",
     "id": "/m/023jgs",
     "creator": "/user/froderik",
     "timestamp": "2007-06-07T19:40:56.000Z"
    },
    {
     "text": "Feyd-Rautha",
     "lang": "en",
     "id": "/m/03pg3c",
     "creator": "/user/froderik",
     "timestamp": "2007-06-07T19:40:43.000Z"
    },
    {
     "text": "Vladimir Harkonnen",
     "lang": "en",
     "id": "/m/026h0",
     "creator": "/user/froderik",
     "timestamp": "2007-06-07T19:40:34.000Z"
    },
    {
     "text": "Lady Jessica",
     "lang": "en",
     "id": "/m/01ch4s",
     "creator": "/user/merge_bot",
     "timestamp": "2009-09-30T09:18:56.000Z"
    },
    {
     "text": "Leto Atreides I",
     "lang": "en",
     "id": "/m/014wvt",
     "creator": "/user/froderik",
     "timestamp": "2007-06-07T18:43:00.000Z"
    },
    {
     "text": "Paul Atreides",
     "lang": "en",
     "id": "/m/0cw39",
     "creator": "/user/froderik",
     "timestamp": "2007-06-07T18:38:45.000Z"
    }
   ],
   "count": 12.0
  },
  "/book/book/editions": {
   "valuetype": "object",
   "values": [
    {
     "text": "Dune",
     "lang": "en",
     "id": "/m/04vcdg_",
     "creator": "/user/ts_bot",
     "timestamp": "2008-11-12T22:20:46.002Z"
    },
    {
     "text": "Dune",
     "lang": "en",
     "id": "/m/04vc5pc",
     "creator": "/user/ts_bot",
     "timestamp": "2008-11-12T22:15:28.000Z"
    },
    {
     "text": "Dune",
     "lang": "en",
     "id": "/m/04vc5qg",
     "creator": "/user/ts_bot",
     "timestamp": "2008-11-12T22:15:31.000Z"
    },
    {
     "text": "Dune",
     "lang": "en",
     "id": "/m/04vc5qq",
     "creator": "/user/ts_bot",
     "timestamp": "2008-11-12T22:15:31.001Z"
    },
    {
     "text": "Dune",
     "lang": "en",
     "id": "/m/04vc5qz",
     "creator": "/user/ts_bot",
     "timestamp": "2008-11-12T22:15:31.002Z"
    },
    {
     "text": "Dune",
     "lang": "en",
     "id": "/m/04vc5r6",
     "creator": "/user/ts_bot",
     "timestamp": "2008-11-12T22:15:32.000Z"
    },
    {
     "text": "Dune",
     "lang": "en",
     "id": "/m/04vc5rh",
     "creator": "/user/ts_bot",
     "timestamp": "2008-11-12T22:15:32.001Z"
    },
    {
     "text": "Dune",
     "lang": "en",
     "id": "/m/04vc5rr",
     "creator": "/user/ts_bot",
     "timestamp": "2008-11-12T22:15:33.000Z"
    },
    {
     "text": "Dune",
     "lang": "en",
     "id": "/m/04vc5r_",
     "creator": "/user/ts_bot",
     "timestamp": "2008-11-12T22:15:33.001Z"
    },
    {
     "text": "Dune",
     "lang": "en",
     "id": "/m/04vc5s7",
     "creator": "/user/ts_bot",
     "timestamp": "2008-11-12T22:15:33.002Z"
    }
   ],
   "count": 55.0
  },
  "/book/book/genre": {
   "valuetype": "object",
   "values": [
    {
     "text": "Science Fiction",
     "lang": "en",
     "id": "/m/06n90",
     "creator": "/user/froderik",
     "timestamp": "2007-06-07T18:38:21.000Z"
    },
    {
     "text": "Fiction",
     "lang": "en",
     "id": "/m/02xlf",
     "creator": "/user/ts_bot",
     "timestamp": "2008-12-03T20:55:17.000Z"
    },
    {
     "text": "Planetary romance",
     "lang": "en",
     "id": "/m/04chq5",
     "creator": "/user/mw_template_bot",
     "timestamp": "2011-11-03T04:15:31.000Z"
    },
    {
     "text": "Military science fiction",
     "lang": "en",
     "id": "/m/01smf2",
     "creator": "/user/mw_template_bot",
     "timestamp": "2011-11-03T04:36:17.000Z"
    },
    {
     "text": "Philosophy",
     "lang": "en",
     "id": "/m/037mh8",
     "creator": "/user/mw_template_bot",
     "timestamp": "2011-11-03T04:45:38.001Z"
    },
    {
     "text": "Conspiracy fiction",
     "lang": "en",
     "id": "/m/0594kx",
     "creator": "/user/mw_template_bot",
     "timestamp": "2012-01-08T15:28:48.003Z"
    },
    {
     "text": "Adventure fiction",
     "lang": "en",
     "id": "/m/08sdrw",
     "creator": "/user/mw_template_bot",
     "timestamp": "2012-01-08T18:26:11.003Z"
    },
    {
     "text": "Psychological thriller",
     "lang": "en",
     "id": "/m/09blyk",
     "creator": "/user/wikimapper",
     "timestamp": "2013-02-26T22:00:22.001Z"
    },
    {
     "text": "Political thriller",
     "lang": "en",
     "id": "/m/02qfv5d",
     "creator": "/user/wikimapper",
     "timestamp": "2013-08-15T23:12:47.000Z"
    },
    {
     "text": "Fantasy",
     "lang": "en",
     "id": "/m/01hmnh",
     "creator": "/user/wikimapper",
     "timestamp": "2014-04-30T09:08:07.000Z"
    }
   ],
   "count": 10.0
  },
  "/book/written_work/author": {
   "valuetype": "object",
   "values": [
    {
     "text": "Frank Herbert",
     "lang": "en",
     "id": "/m/02xyl",
     "creator": "/user/gardening_bot",
     "timestamp": "2008-04-23T15:30:11.001Z"
    }
   ],
   "count": 1.0
  },
  "/book/written_work/copyright_date": {
   "valuetype": "datetime",
   "values": [
    {
     "text": "1965",
     "lang": "",
     "value": "1965",
     "creator": "/user/gardening_bot",
     "timestamp": "2008-03-14T03:45:20.018Z"
    }
   ],
   "count": 1.0
  },
  "/book/written_work/date_of_first_publication": {
   "valuetype": "datetime",
   "values": [
    {
     "text": "1965",
     "lang": "",
     "value": "1965",
     "creator": "/user/ewelinab",
     "timestamp": "2013-11-05T11:21:32.000Z"
    }
   ],
   "count": 1.0
  },
  "/book/written_work/date_written": {
   "values": [],
   "count": 0.0,
   "status": "has_value"
  },
...

【问题讨论】:

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


    【解决方案1】:

    这似乎是来自 RestSharp dll 的 json 反序列化程序的内部反序列化问题。 使用Json.NET,完美运行:

    【讨论】:

    • 谢谢尤瓦尔!在您的测试用例中,您是否仍然使用 RestSharp 连接 Freebase API?
    • 是的,我只替换了反序列化位。我建议你看看HttpClient,这样你就可以完全消除对RestSharp的依赖
    【解决方案2】:

    这是基于 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);
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多