0

嗨,我正在尝试创建 Web api,我可以调用它来查看 MongoDB 中的所有文档,现在文档非常大并且嵌套严重,我已设法返回文档,但在 Json 中,标题为 XML。

我需要在 Json 中返回这整件事!

此代码采用 BsonDocument 产品并将其作为 Json 返回,因为没有它我会收到错误:

[JsonIgnore]
        public BsonDocument Product { get; set; }

[DataMember]
        public string Product
        {
            get { return Product .ToJson(); }
            set { Product = BsonDocument.Parse(value); }
        }

这是文档的示例(这是一个基本示例,实际文档要大得多,层次更深:

{
    "product": {
        "Type": "Phone",
            "Size": {
            "Height": 10,
                "Lenght": 5,
                "Weight": 30
        }
        "Make": "Apple"
        "Model": {
            "Name": "IPhone",
                "Range": "4s"
        }

    }
}

它返回为

<Product>
{"product": {"Type": "Phone","Size": {"Height": 10,"Lenght": 5,"Weight": 30}"Make": "Apple", "Model": {"Name": "IPhone","Range": "4s"}}}
</Product>

我该如何解决?

4

1 回答 1

1

我该如何解决?

像这样:

public HttpResponseMessage Get()
{
    MyViewModel model = ...
    // This will contain the JSON you want to return to the client
    string product = model.Product;

    var response = new HttpResponseMessage();
    response.Content = new StringContent(product, Encoding.UTF8, "application/json");
    return response;
}
于 2013-01-25T11:36:21.710 回答