使用RestSharp Client

我想使用REST Api并反序列化嵌套的JSON响应。我使用json2csharp.com创建了这些类。

要使用的JSON

{
  id: 32,name: "test supply object",formId: 4,sortOrder: 0,created: 1572902163907,creator: "jsingler",attributes: [
    {
      id: 144,attribute: {
        attributeName: "Description",attributeType: "TextArea",id: 8
      },attributeValue: "for development testing. do not delete or use."
    },{
      id: 145,attribute: {
        attributeName: "Quantity",attributeType: "NumberLong",id: 10
      },attributeValue: "4"
    }
  ]
}

JSON2CSHARP.COM输出

public partial class Asset
{
    [JsonProperty(PropertyName = "id")]
    public int id { get; set; }
    [JsonProperty(PropertyName = "name")]
    public string name { get; set; }
    [JsonProperty(PropertyName = "formId")]
    public int formId { get; set; }
    [JsonProperty(PropertyName = "sortOrder")]
    public int sortOrder { get; set; }
    [JsonProperty(PropertyName = "created")]
    public long created { get; set; }
    [JsonProperty(PropertyName = "creator")]
    public string creator { get; set; }
    public List<Attribute> attributes { get; set; }
}

public partial class Attribute
{
    [JsonProperty(PropertyName = "ida")]
    public int id { get; set; }
    [JsonProperty(PropertyName = "attribute")]
    public List<Attribute1> attribute { get; set; }
    [JsonProperty(PropertyName = "attributeValue")]
    public string attributeValue { get; set; }
}

public partial class Attribute1
{
    [JsonProperty(PropertyName = "attributeName")]
    public string attributeName { get; set; }
    [JsonProperty(PropertyName = "attributeType")]
    public string attributeType { get; set; }
    [JsonProperty(PropertyName = "ida1")]
    public int id { get; set; }
}

使用API​​数据的方法

public static List<Asset> GetallAssets()
{
    var client = new RestClient("URL_USED");
    var request = new RestRequest();
    client.Authenticator = new HttpBasicAuthenticator("username","PW");
    var response = new RestResponse();
    Task.Run(async () =>
    {
        response = await GetResponseContentAsync(client,request) as RestResponse;
    }).Wait();

    var AssetList = JsonConvert.DeserializeObject<List<Asset>>(response.Content);

    return AssetList;
}

总是会出现以下错误: An unhandled exception occurred while processing the request. JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[PVF_Web.Models.Attribute1]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer,not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path '[0].attributes[0].attribute.attributeName',line 1,position 158.

然后,当我能够对JSON进行反序列化时,它不会返回属性(应用程序的真正目的)。

任何建议或想法都会受到赞赏。

编辑1

[HttpPut]
public void Send(Asset newJA,int Qty)
{
    var client = new RestClient("URL_TO_USE" + newJA.id + ".json");
    var request = new RestRequest("",Method.PUT);
    request.AddObject(newJA);

    client.Authenticator = new HttpBasicAuthenticator("username","PW");
    client.ExecuteAsync(request,response => {
          Console.WriteLine(response.Content);
          Debug.WriteLine(response.Content);
     });
}

这总是失败。

aiguoai 回答:使用RestSharp Client

您的JSON无效。转到https://jsonformatter.curiousconcept.com/并检查所有错误。

属性名称必须在引号之间,并且如果要使其成为数组,则需要使其成为一个数组,并在json的开头和结尾添加方括号-[]-。

,

首先请确保您的json有效。您可以看看这个side json属性必须在引号内“”像这样

  

“ id”

除此之外,您的根对象不是list。因此,您必须将反序列化代码更改为:

var AssetList = JsonConvert.DeserializeObject<Asset>(response.Content);
本文链接:https://www.f2er.com/3143794.html

大家都在问