【问题标题】:Someone can use array of json in .net?有人可以在.net中使用json数组吗?
【发布时间】:2015-08-28 13:03:36
【问题描述】:

我正在尝试将我的应用程序与 mercadolibre API 集成。我可以毫无问题地列出一个新项目,但是当我尝试插入图片数组时,集成不起作用。 错误信息:

{"message":"body.invalid_field_types","error":"[invalid property type: [pictures] expected List but was null value: [pictures:[[source:http://www.nichepursuits.com/wp-content/uploads/2014/02/buy-websites.jpg], [source:http://thumb9.shutterstock.com/display_pic_with_logo/639229/176602316/stock-vector-best-buy-rubber-stamp-red-icon-isolated-on-white-background-vector-illustration-176602316.jpg]]]]","status":400,"cause":[]}

当我尝试使用下面的第二个选项时,Visual Studio 显示编译错误。

我已经测试了很多声明,但都失败了。

暂定1:

IRestResponse r = m.Post("/items", ps, new
                {
                    title = _item.title,
                    category_id = _item.category_id,
                    price = _item.price,
                    currency_id = _item.currency_id,
                    available_quantity = _item.available_quantity,
                    buying_mode = _item.buying_mode,
                    listing_type_id = _item.listing_type_id,
                    condition = _item.condition,
                    description = _item.description,
                    warranty = _item.warranty,
                    pictures = new {pictures = new [] 
                    {
                        new {source = "http://www.nichepursuits.com/wp-content/uploads/2014/02/buy-websites.jpg"}, 
                        new {source = "http://thumb9.shutterstock.com/display_pic_with_logo/639229/176602316/stock-vector-best-buy-rubber-stamp-red-icon-isolated-on-white-background-vector-illustration-176602316.jpg"}
                    }}
                });

暂定 2:

IRestResponse r = m.Post("/items", ps, new
            {
                title = _item.title,
                category_id = _item.category_id,
                price = _item.price,
                currency_id = _item.currency_id,
                available_quantity = _item.available_quantity,
                buying_mode = _item.buying_mode,
                listing_type_id = _item.listing_type_id,
                condition = _item.condition,
                description = _item.description,
                warranty = _item.warranty,
                pictures = [{"Source":"Value"},{"Source":Value2}]

            });

【问题讨论】:

  • 描述“不工作”。你有错误吗?它怎么不能完成你想要的?
  • 嗨,梅森。我已经编辑了这个主题。谢谢。
  • 你说现在抛出编译错误,但是你需要解释编译错误是什么。
  • 在第一个代码块上pictures 加倍:pictures = new {pictures = new [] ...。试试pictures = new [] ....
  • 谢谢。解决方案如下。

标签: asp.net arrays json


【解决方案1】:

C# 对 JSON 数组一无所知,幸运的是,它有自己的。

您的代码:

 pictures = new {pictures = new [] 
                {
                    new {source = "http://www.nichepursuits.com/wp-content/uploads/2014/02/buy-websites.jpg"}, 
                    new {source = "http://thumb9.shutterstock.com/display_pic_with_logo/639229/176602316/stock-vector-best-buy-rubber-stamp-red-icon-isolated-on-white-background-vector-illustration-176602316.jpg"}
                }}

这实际上不是一个数组,而是匿名对象。 尝试重写它:

pictures = new[] 
{
    new { source = "http://www.nichepursuits.com/wp-content/uploads/2014/02/buy-websites.jpg" },
    new { source = "http://thumb9.shutterstock.com/display_pic_with_logo/639229/176602316/stock-vector-best-buy-rubber-stamp-red-icon-isolated-on-white-background-vector-illustration-176602316.jpg"}
}

甚至只是字符串数组:

pictures = new string[] 
{
    "http://www.nichepursuits.com/wp-content/uploads/2014/02/buy-websites.jpg",
    "http://thumb9.shutterstock.com/display_pic_with_logo/639229/176602316/stock-vector-best-buy-rubber-stamp-red-icon-isolated-on-white-background-vector-illustration-176602316.jpg"
}

第二个选项: pictures = [{"Source":"Value"},{"Source":Value2}] 不是一个有效的行,你不能用 C# 写这个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 2021-08-29
    • 2011-08-20
    • 2010-09-29
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    相关资源
    最近更新 更多