开发者

Access MongoDB Rest service with C#

I'm a beginner with mongoDb, I would like to access MongoDb rest service, th开发者_如何学JAVAe data I retrieved is json type. My question is, how do you parse this data ? I don't find any MongoDb api which allows me to query it easily. So what would you do ?

Here's an example of the data, I queried the key "Name" which returned me on row thanks to this url: http://localhost:28017/MyDatabase/MyCollection/?filter_Key=Name

{ "offset" : 0, "rows": [ { "_id" : { "$binary" : "fXvnbtlMhU24EWg9NiY5QQ==", "$type" : "03" }, "Key" : "Name", "Value" : "John Smith" } ],

"total_rows" : 1 , "query" : { "Key" : "Name" } , "millis" : 0 }

And I would like to retrieve the Value "John Smith"

Thank's

[EDIT] I've managed to get {"Value": "John Smith"} out of my json. oh!! See this ugly code:

var urlToFetch = "http://`localhost`:28017/MyDatabase/MyCollection/?filter_Key=Name";
var jsonData = GetDataFrom(urlToFetch);
var value = JsonConvert.DeserializeObject(jsonData);
foreach (var key in ((JObject)value)["rows"].Values())
{
    key.Parent.Last;
}

It's not perfect, I still don't get my John Smith But there're must be a better way without manually parsing, aren't there ?


Here the solution guys:

public class yourclass
{
    public void transformJsonToObject()
    {
     JsonSerializer serializer = new JsonSerializer();
     var value = JsonConvert.DeserializeObject<ResultViewModel>(jsonData);
     }
}
public class ResultViewModel
{
    [JsonProperty("offset")]
    public int Offset;
    [JsonProperty("rows")]
    public TestViewModel[] Rows;
}

public class TestViewModel
{
   [JsonProperty("_id")]
    public TestArray Id { get; set; }
    public string Key { get; set; }
    public string Value { get; set; }
}

public class TestArray
{
    [JsonProperty("$binary")]
    public string Binary { get; set; }
    [JsonProperty("$type")]
    public string Type { get; set; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜