gpt4 book ai didi

南希使用 JSON 的复杂对象不起作用

转载 作者:行者123 更新时间:2023-12-01 13:56:52 26 4
gpt4 key购买 nike

客户端代码:

 var basket = {
products: [],
user: { name: "schugh" }
};

$("#basket table tr").each(function (index, item) {
var product = $(item).data('product');
if (product) {
basket.products.push(product);
}

});

$.ajax({
url: "http://localhost:12116/basketrequest/1",
async: true,
cache: false,
type: 'POST',
data: JSON.stringify(basket),
contentType: 'application/json; charset=utf-8',
dataType: 'json',

success: function (result) {
alert(result);
},
error: function (jqXHR, exception) {
alert(exception);
}
});

服务器代码:

 Post["/basketrequest/{id}"] = parameters =>
{
var basketRequest = this.Bind(); //basketrequest is null
return Response.AsJson(basketRequest , HttpStatusCode.OK);
};

其他模型类:

[Serializable]
public class BasketRequest
{
public User User;
public List<Product> Products;
}

public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
public ProductStatus ProductStatus { get; set; }
}

public enum ProductStatus
{
Created,
CheckedBy,
Published
}

public class User
{
public string Name { get; set; }
}

Nancy 模块中的代码 this.Bind();返回 null .如果我将 Complex 对象更改为 List<Product> ,即没有 wrapper BasketRequest , 对象很好...

有什么建议吗?

编辑:发布的 JSON:

{
"User": {
"Name": "SChugh"
},
"Products": [{
"Id": 1,
"Name": "Tomato Soup",
"Category": "Groceries",
"Price": 1
}, {
"Id": 2,
"Name": "Yo-yo",
"Category": "Toys",
"Price": 3.75
}]
}

最佳答案

您的BasketRequest 对象应该实现属性而不是字段。所以

public class BasketRequest
{
public User User { get; set; }
public List<Product> Products { get; set; }
}

你也应该使用泛型方法

this.Bind<BasketRequest>();

关于南希使用 JSON 的复杂对象不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24677737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com