反序列化json时出现无效的Json字符串错误

时间:2018-09-05 03:16:36

标签: asp.net-mvc asp.net-web-api2 asp.net-core-webapi json-deserialization

我正在研究ASP.NET MVC项目。当我尝试使用移动客户端从API检索数据时,出现此错误:

  

System.Runtime.Serialization.SerializationException:无效的JSON字符串

API正在运行-我已使用浏览器对其进行了测试。 json反序列化时出现此错误。

这是我调用API的方法

public string GetAgreementText(long idCompany)
{
            string agreementText = "";
            _tokenService.RenewToken(_deviceId);
           // var token = _tokenService.GetLastActivateToken().Hash;
            var clientURL = CrossSettings.Current.GetValueOrDefault("API_ENDPOINT", "");
            var client = new RestClient(clientURL);
            var request = new RestRequest("GetAgreementText/{id}", Method.GET);

           // request.AddHeader("Authorization", $"Bearer {token}");
           // request.AddHeader("Content-type", "application/x-www-form-urlencoded");
            request.AddUrlSegment("Id", idCompany.ToString());

            IRestResponse response = client.Execute(request);
            AppRestResponse apiResponse = SimpleJson.DeserializeObject<AppRestResponse>(response.Content);

            var statusMessage = "";

            if (apiResponse.Success)
            {
                statusMessage = "Success.";
                if (!string.IsNullOrEmpty(response.Content))
                {
                    agreementText = SimpleJson.DeserializeObject<string>(response.Content);
                }
                else
                {
                    throw new Exception("Invalid response");
                }
            }
            else
            {
                agreementText = "Error retrieving agreement text";
            }

            return agreementText;
}

API方法

[HttpGet]
        [Route("GetAgreementText/{id}")]
        public DefaultApiResult GetAgreementText(long Id)
        {
            Company com = _companyRepository.Get(Id);
            string st = com.AgreementText;

            DefaultApiResult result = new DefaultApiResult
            {
                Data = st
            };
            return result;
        }

0 个答案:

没有答案