代码之家  ›  专栏  ›  技术社区  ›  waleed

无法使用[]对对象类型的表达式应用索引

  •  0
  • waleed  · 技术社区  · 11 月前

    我正在为.netwindows表单上的API工作,我从提供的网站复制了代码,发现了错误 say“无法对'object'类型的表达式应用[]索引”如何修复此问题 这是我的代码

      RestClient restClient = new RestClient("https://api.wassenger.com");
      RestRequest restRequest = new RestRequest("/v1/files");
       restRequest.Method = Method.Post;
       restRequest.AddHeader("Token", "122eb0a65870e715a1de9ad06d11881b841e6db485bd387");
       restRequest.AddHeader("Content-Type", "multipart/form-data");
       restRequest.AddFile("file", "/path/to/image.jpg");
    
      var response = restClient.Execute(restRequest);
      var json = JsonConvert.DeserializeObject(response.Content);
     //error at line below (string)json[0]["id]
     var fileId = (string)json[0]["id"];
    
    Console.WriteLine("File uploaded successfully with ID: {0}", fileId);
    

    返回的响应如下

     [
     {
    "id": "606f888ed01a7a65946c0701",
    "format": "native",
    "filename": "sample.jpg",
    "size": 53697,
    "mime": "image/jpeg",
    "ext": "jpeg",
    "kind": "image",
    "sha2":   "305fc37036ffd53aec6d8c4512c0114fd38ac52f85d334e29647a21f0835c801",
    "tags": [],
    "status": "active",
    "mode": "default",
    "createdAt": "2021-05-08T22:49:50.954Z",
    "expiresAt": "2021-09-06T22:49:50.949Z"
    }
    ]
    
    1 回复  |  直到 11 月前
        1
  •  0
  •   Yong Shun    11 月前

    假设您的响应是一个JSON数组,您应该将其反序列化为 List<T> 哪一个 T 是基于JSON定义的类。

    您可以使用 Visual Studio Json2Csharp 将JSON转换为类。

    public class Root
    {
        [JsonProperty("id")]
        public string Id { get; set; }
    
        [JsonProperty("format")]
        public string Format { get; set; }
    
        [JsonProperty("filename")]
        public string Filename { get; set; }
    
        [JsonProperty("size")]
        public int Size { get; set; }
    
        [JsonProperty("mime")]
        public string Mime { get; set; }
    
        [JsonProperty("ext")]
        public string Ext { get; set; }
    
        [JsonProperty("kind")]
        public string Kind { get; set; }
    
        [JsonProperty("sha2")]
        public string Sha2 { get; set; }
    
        [JsonProperty("tags")]
        public List<object> Tags { get; set; }
    
        [JsonProperty("status")]
        public string Status { get; set; }
    
        [JsonProperty("mode")]
        public string Mode { get; set; }
    
        [JsonProperty("createdAt")]
        public DateTime CreatedAt { get; set; }
    
        [JsonProperty("expiresAt")]
        public DateTime ExpiresAt { get; set; }
    }
    
    string fileId = String.Empty;
    
    List<Root> list = JsonConvert.DeserializeObject<List<Root>>(myJsonResponse);
    
    if (list != null && list.Count > 0)
    {
        fileId = list[0].Id;
    }
    

    或者你可以与 JArray .

    using Newtonsoft.Json.Linq;
    
    string fileId = String.Empty;
    
    var jArray = JArray.Parse(response.Content);
    if (jArray != null && jArray.Count > 0)
    {
        fileId = jArray[0]["id"].ToString();
        // Alternative methods    
        // fileId = jArray[0].Value<string>("id"));
        // fileId = jArray[0].SelectToken("id").ToString());
    }