【发布时间】:2012-09-07 07:11:41
【问题描述】:
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.
我确实尝试将列表转换为数组...即使感觉不对...也失败了...此外,为什么我必须使用 RavenJObject 和 RavenJArray 而不是Json.net 附带的默认值.. 我猜引擎盖下正在进行一些漂亮的工作..
【问题讨论】:
-
看起来 [What Do I need to store objects in RavenDB][1] 中给出的答案可能会对您有所帮助。 [1]:stackoverflow.com/questions/6514491/…