JsonSerializationException:无法将当前JSON对象(例如{“ name”:“ value”})反序列化为type

时间:2019-06-07 00:23:56

标签: asp.net-web-api azure-cosmosdb asp.net-core-webapi

我正在开发网络api。出现以下错误。相同的代码结构适用于fetchbyid,post,edit。我在这里错了什么。请帮我。 Catalog.cs:

  public class Catalog
    {
        [JsonProperty("id")]
        public Guid? Id { get; set; }

        [JsonProperty("VendorName")]
        public string VendorName { get; set; }

       // public List<Industy> Industy { get; set; }
        public Industy Industy { get; set; }

        public Catalog()
        {
            if (Id == null)
            {
                Id = Guid.NewGuid();
            }
            else
            {
                Id = Id;
            }

         //   this.Industy = new List<Industy>();
        }
    }




   public async Task<IEnumerable<Catalog>> FetchListAsync(
           Guid? itemId)
        {
            var feedOptions =
                  new FeedOptions
                  {
                      MaxItemCount = -1,
                      EnableCrossPartitionQuery = true
                  };
            var query = new SqlQuerySpec
            {
                QueryText = "SELECT * FROM c"

            };

            var orderDocumentQuery =
                _cosmosClient.CreateDocumentQuery<Catalog>(
                    UriFactory.CreateDocumentCollectionUri(
                        _azureCosmosDbOptions.Value.DatabaseId, "catalog"), query, feedOptions)
                    .AsDocumentQuery();

            var orderList =
                new List<Catalog>();
            Console.WriteLine(orderDocumentQuery.ToString());

            while (orderDocumentQuery.HasMoreResults)
            {
                orderList.AddRange(
                    await orderDocumentQuery.ExecuteNextAsync<Catalog>());
            }

            return orderList;
        }

错误:

  

JsonSerializationException:无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型'System.Collections.Generic.List`1 [CatalogAPI.Entities.Industy]',因为该类型需要JSON数组(例如[1,2,3])正确反序列化。   要解决此错误,可以将JSON更改为JSON数组(例如[1,2,3]),也可以更改反序列化类型,使其成为普通的.NET类型(例如,不像整数这样的原始类型,也不像这样的集合类型)可以从JSON对象反序列化的数组或List<T>)。还可以将JsonObjectAttribute添加到类型中,以强制其从JSON对象反序列化。   路径“ Industy.Id”,第1行,位置117。

我在做什么错??

1 个答案:

答案 0 :(得分:2)

Cosmos DB中的POCO对象需要将其id字段作为字符串。

您需要将public Guid? Id { get; set; }替换为public string Id { get; set; }