1

我正在尝试阅读 Rest Web API 响应。解析数据时出现错误

响应内容类型:application/json;字符集=utf-8

响应:{"access_token":"ot-a4943ac25cf94df3adacd11c71b8ea01","token_type":"Bearer","scope":["READ_USER_PROFILE","WRITE_CONVERSATIONS","READ_CONVERSATIONS"]}

DTO:

public class ResponseData
    {
        public string AccessToken { get; set; }
        public string TokenType { get; set; }
        public string Scope { get; set; }
    }

public class Scope
    {
        public string[] RequestScope { get; set; }
    }

代码:

using (HttpContent rescontent = response.Result.Content)
            {
                // ... Read the string.
                Task<string> result = rescontent.ReadAsStringAsync();
               // res = result.Result;

                var responseData = JsonConvert.DeserializeObject<ResponseData>(result.Result);

                Console.WriteLine("Response: {0}", responseData.AccessToken);
            }

错误:Newtonsoft.Json.JsonReaderException:'解析值时遇到意外字符:[。路径“范围”,第 1 行,位置 85。

编程语言:C#

请告诉我,如何纠正这个问题?我想从响应中读取访问令牌参数。

4

1 回答 1

1

您缺少json中的和表示的数组[]

{ 
   "access_token":"ot-a4943ac25cf94df3adacd11c71b8ea01",
   "token_type":"Bearer",
   "scope":[ // this is an array
      "READ_USER_PROFILE",
      "WRITE_CONVERSATIONS",
      "READ_CONVERSATIONS"
   ]
}

你需要修改你的类

public List<string> Scope { get; set; }

或者

public string[] Scope { get; set; }

编辑

您可能还有其他错误,因此您可能需要更改为

public string Access_Token { get; set; }
public string Token_Type { get; set; }

或者

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

[JsonProperty("token_type")]
public string TokenType { get; set; }
于 2020-02-03T08:50:23.763 回答