如何检索 mongo db 对象并将其作为 json 对象返回

发布于 2024-11-11 01:48:08 字数 1896 浏览 1 评论 0原文

您好,我将其作为我的控制器之一:

[HttpPost]
        public JsonResult GetPinPoints(string Id)
    {
        Frames rslt = null;

        string connString = ConfigurationManager.ConnectionStrings["MongoConnStringNew"].ToString();
        MongoUrl murl = new MongoUrl(connString);
        MongoServer mgconf = new MongoServer(murl);
        try
        {
            mgconf.Connect();
            MongoDatabase frmlydb = mgconf.GetDatabase("framely");
            MongoCollection<Frames> collection = frmlydb.GetCollection<Frames>("Frames");
            ObjectId oid = new ObjectId(Id);
            Frames frms = collection.FindOne(Query.EQ("_id", oid));

            if (frms != null)
            {
                rslt = frms;
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            mgconf.Disconnect();
        }

        return Json(rslt.CoordinatesObj.ToJson());
    }

mongo 对象如下所示:

{"MetaTagsObj":{"Meta1":"my fam","Meta2":"lololo","Meta3":"lulz"},"PictureID":"http://framely.s3.amazonaws.com/0b7a9a72-c61b-4dec-a814-40b003072e31.jpg","UserID":"1","CoordinatesObj":[{"Position":1,"Top":182,"Left":20,"Height":73,"Width":90},{"Position":2,"Top":69,"Left":103,"Height":98,"Width":1...

我使用 ajax jquery 函数来调用如下所示的控制器:

$("#mybutton").click(function(){
           $.ajax({
                url: '/Member/GetPinPoints',
                type: "POST",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function(data) {
            alert(data);
            },
            error: function() {

            alert("error");
            }
            });


    return false;
});

我认为我做得不对,我认为这与方式有关我返回 json 对象。我不断收到此错误:

{“对象引用未设置为对象的实例。”}

就在我返回 Json 对象的位置。

Hi I have this as one of my controllers:

[HttpPost]
        public JsonResult GetPinPoints(string Id)
    {
        Frames rslt = null;

        string connString = ConfigurationManager.ConnectionStrings["MongoConnStringNew"].ToString();
        MongoUrl murl = new MongoUrl(connString);
        MongoServer mgconf = new MongoServer(murl);
        try
        {
            mgconf.Connect();
            MongoDatabase frmlydb = mgconf.GetDatabase("framely");
            MongoCollection<Frames> collection = frmlydb.GetCollection<Frames>("Frames");
            ObjectId oid = new ObjectId(Id);
            Frames frms = collection.FindOne(Query.EQ("_id", oid));

            if (frms != null)
            {
                rslt = frms;
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            mgconf.Disconnect();
        }

        return Json(rslt.CoordinatesObj.ToJson());
    }

The mongo object looks like this:

{"MetaTagsObj":{"Meta1":"my fam","Meta2":"lololo","Meta3":"lulz"},"PictureID":"http://framely.s3.amazonaws.com/0b7a9a72-c61b-4dec-a814-40b003072e31.jpg","UserID":"1","CoordinatesObj":[{"Position":1,"Top":182,"Left":20,"Height":73,"Width":90},{"Position":2,"Top":69,"Left":103,"Height":98,"Width":1...

I use an ajax jquery function to call the controller that looks like this:

$("#mybutton").click(function(){
           $.ajax({
                url: '/Member/GetPinPoints',
                type: "POST",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function(data) {
            alert(data);
            },
            error: function() {

            alert("error");
            }
            });


    return false;
});

I dont think I am doing this right, I think it has to do with the way I return the json object. I keep getting this error:

{"Object reference not set to an instance of an object."}

right where I am returning the Json object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的痛♀有谁懂 2024-11-18 01:48:08

return Json(myObject) 语句应该采用一个对象,该对象将被序列化为 JSON,然后作为字符串返回到浏览器,但通过调用 ToJson() >rslt.CoordinatesObj 对象将被序列化两次。

也有可能 CooperativesObj 没有被正确反序列化,因此它会抛出异常,因为 ToJson() 是在 null 对象上调用的。
Frames 类应如下所示来处理 CooperativesObj 数组的反序列化:

public class Frames
{
    IEnumerable<Coordinate> CoordinatesObj { get; set; }

    public class Coordinate
    {
        int Position { get; set; }
        int Top { get; set; }
        int Left { get; set; }
        int Height { get; set; }
        int Width { get; set; }
    }
}

The return Json(myObject) statement should take an object that'll be serialised to JSON then returned to the browser as a string, but by calling ToJson() the rslt.CoordinatesObj object will be serialised twice.

It's also possible CoordinatesObj isn't being deserislised properly, so it's throwing an exception because ToJson() is called on a null object.
The Frames class should look something like this to handle deserialising the CoordinatesObj array:

public class Frames
{
    IEnumerable<Coordinate> CoordinatesObj { get; set; }

    public class Coordinate
    {
        int Position { get; set; }
        int Top { get; set; }
        int Left { get; set; }
        int Height { get; set; }
        int Width { get; set; }
    }
}
小红帽 2024-11-18 01:48:08

如果 FindOne 找不到匹配的文档,它将返回 null,因此在您的代码示例中,“rslt”变量完全有可能为 null。

另外:

  1. 不需要调用 Connect,驱动程序会自动连接,
  2. 不要调用 Disconnect,它会干扰
    带连接池

If FindOne doesn't find a matching document it returns null, so in your code sample it is entirely possible for the "rslt" variable to be null.

Also:

  1. no need to call Connect, the driver connects automatically
  2. dont' call Disconnect, it interferes
    with connection pooling
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文