2
public class Question
    {
        public int Id { get; set; }
        public int? Score { get; set; }
        public string Title { get; set; }
        public List<string> Tags { get; set; }
        public Owner Owner { get; set; }
        public Uri Link { get; set; }
        public bool IsAnswered { get; set; }
    }

public class Owner
    {
        public int? ID { get; set; }
        public String Name { get; set; }
        public int Reputation { get; set; }
        public Uri Link { get; set; }
    }

public static dynamic CallStackOverFlow()
        {
            var client = new RestClient("https://api.stackexchange.com/2.1");
            var request = new RestRequest("/search/advanced", Method.GET);
            request.RequestFormat = DataFormat.Json;
            request.AddParameter("site", "stackoverflow");
            request.AddParameter("tagged", "C#");
            request.AddParameter("pagesize", "1");
            var response = client.Execute(request);
            var content = response.Content; // raw content as string
            dynamic deserialised = JsonConvert.DeserializeObject(content);
            return deserialised;
        }

//go call stackoverflow
            var d = StackOverflow.CallStackOverFlow();
            var questions = new List<Question>();
            foreach (var q in d.items)
            {
                Console.WriteLine(q);
                var question = new Question
                {
                    Id = q.question_id,
                    IsAnswered = q.is_answered,
                    Link = new Uri(q.link == null ? "" : (string)q.link),
                    Owner = new Owner
                    {
                        ID = q.owner.user_id,
                        Link = new Uri(q.owner.link == null ? "" : (string)q.owner.link),
                        Name = q.owner.display_name,
                        Reputation = q.owner.reputation
                    },
                    Tags = (q.tags as JArray).Values().Select(v => v.ToString()).ToList(),
                    Score = q.score,
                    Title = q.title
                };

                questions.Add(question);
            }
            using (IDocumentStore documentStore = new DocumentStore() { Url = "http://localhost:8080", DefaultDatabase = "StackOverflow" })
            {
                documentStore.Initialize();
                using (IDocumentSession session = documentStore.OpenSession())
                {
                    session.Store(questions);
                    session.SaveChanges();
                }
            } 

我刚开始使用 RavenDb,如果这听起来有点愚蠢,请原谅我。我浏览了客户端 api...无法弄清楚为什么会出现以下异常..

Object serialized to Array. RavenJObject instance expected.

我确实尝试将列表强制转换为数组...即使感觉不对..也失败了..此外,为什么我必须使用RavenJObjectandRavenJArray而不是附带的默认值Json.net..我是猜测引擎盖下正在进行漂亮的工作..

4

1 回答 1

6

发生异常是因为您在调用时尝试存储一系列问题:session.Store(questions);

该 api 期望接收一个实体而不是数组或集合。

您可能希望使用批处理操作来处理一堆问题的加载:http ://ravendb.net/docs/client-api/advanced/databasecommands/batch

于 2012-09-06T23:11:41.643 回答