使用C#访问MongoDB Rest服务
我是 mongoDb 的初学者,我想访问 MongoDb Rest 服务,我检索的数据是 json 类型。我的问题是,你如何解析这些数据?我没有找到任何 MongoDb api 可以让我轻松查询它。那么你会怎么做?
这是数据的示例,我查询了键“Name”,由于以下网址,该键将我返回到行: http://localhost
:28017/MyDatabase/MyCollection/?filter_Key=名称
{ “偏移量”:0, “行”:[ { "_id" : { "$binary" : "fXvnbtlMhU24EWg9NiY5QQ==", "$type" : "03" }, "键" : "名称", "值" : "约翰·史密斯" } ],
"总行数": 1 , “查询”:{“键”:“名称”}, “毫利斯”:0 ]
我想检索值“John Smith”
谢谢
[编辑 我已经设法从我的 json 中获取 {"Value": "John Smith"} 。哦!!看看这个丑陋的代码:
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;
}
它并不完美,我仍然没有得到我的 John Smith 但一定有一个更好的方法而无需手动解析,不是吗?
I'm a beginner with mongoDb, I would like to access MongoDb rest service, the 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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的解决方案伙计们:
Here the solution guys: