无法使用ReadAsAsync <t>获取对象

时间:2018-07-31 07:02:13

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

我有一个Product类,如下所示:

Public class Product
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Code { get; set; }
    public string Barcode { get; set; }
    public string InnerCode { get; set; }

    public virtual ProductUnit ProductUnit { get; set; }
    public int? ProductUnitID { get; set; }

    public virtual ProductType ProductType { get; set; }
    public int? ProductTypeID { get; set; }

}

在ASP.NET Core Web API服务中,我有一个put方法,该方法返回OK(product)

邮递员的回复如下:

{
"result": {
    "id": 22,
    "name": "Bread",
    "productType": {
        "id": 4,
        "name": "Food",
        "remarks": null,
        "products": []
    },
    "productTypeID": 4,
    "code": "566",
    "barcode": "855",
    "innerCode": "145522",
    "productUnit": {
        "id": 4,
        "name": "Box",
        "remarks": null,
        "products": []
    },
    "productUnitID": 4
},
"id": 592,  ---> //probably this
"exception": null,
"status": 5,
"isCanceled": false,
"isCompleted": true,
"isCompletedSuccessfully": true,
"creationOptions": 0,
"asyncState": null,
"isFaulted": false
}

我正在尝试获取Product对象,如下所示:

var data = await httpResponseMessage.Content.ReadAsAsync<Product>();

但是结果是,我得到的产品对象具有空属性,除了ID之外,ID是随机数,而且我认为这是json响应中异常上方的id。

我有什么错误?

1 个答案:

答案 0 :(得分:0)

所以,我相信您正在尝试解析结果,这是您所遇到的内部对象。 为了解析整个结果,您必须为上述json创建一个类型,您可以使用https://app.quicktype.io/#l=cs&r=json2csharp创建该类型。

课程将如下:

public partial class ProductResult
    {
        [JsonProperty("result")]
        public Result Result { get; set; }

        [JsonProperty("id")]
        public long Id { get; set; }

        [JsonProperty("exception")]
        public object Exception { get; set; }

        [JsonProperty("status")]
        public long Status { get; set; }

        [JsonProperty("isCanceled")]
        public bool IsCanceled { get; set; }

        [JsonProperty("isCompleted")]
        public bool IsCompleted { get; set; }

        [JsonProperty("isCompletedSuccessfully")]
        public bool IsCompletedSuccessfully { get; set; }

        [JsonProperty("creationOptions")]
        public long CreationOptions { get; set; }

        [JsonProperty("asyncState")]
        public object AsyncState { get; set; }

        [JsonProperty("isFaulted")]
        public bool IsFaulted { get; set; }
    }

    public partial class Result
    {
        [JsonProperty("id")]
        public long Id { get; set; }

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

        [JsonProperty("productType")]
        public Product ProductType { get; set; }

        [JsonProperty("productTypeID")]
        public long ProductTypeId { get; set; }

        [JsonProperty("code")]
        [JsonConverter(typeof(ParseStringConverter))]
        public long Code { get; set; }

        [JsonProperty("barcode")]
        [JsonConverter(typeof(ParseStringConverter))]
        public long Barcode { get; set; }

        [JsonProperty("innerCode")]
        [JsonConverter(typeof(ParseStringConverter))]
        public long InnerCode { get; set; }

        [JsonProperty("productUnit")]
        public Product ProductUnit { get; set; }

        [JsonProperty("productUnitID")]
        public long ProductUnitId { get; set; }
    }

    public partial class Product
    {
        [JsonProperty("id")]
        public long Id { get; set; }

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

        [JsonProperty("remarks")]
        public object Remarks { get; set; }

        [JsonProperty("products")]
        public List<object> Products { get; set; }
    }

现在您可以将ProductResult用作

var data = await httpResponseMessage.Content.ReadAsAsync<ProductResult>();

更新

另一种解决方案是,您可以使用JObject类来代替为完整的JSON数据创建类型(类),而可以使用jsonpath来选择任何属性或对象,如下所示:

string result = await  httpResponseMessage.Content.ReadAsStringAsync();
Product product = JObject.Parse(result).SelectToken("$.result").ToObject<Product>()