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