Xamarin C# Json 从 Baselinker 解析值时遇到意外字符。

huangapple go评论72阅读模式
英文:

Xamarin C# Json Unexpected character encountered while parsing value from Baselinker

问题

I am trying to get data from Baselinker.
But I am getting an error

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: S. Path '', line 0, position 0.'

The same in WPF

private async void Button_Clicked(object sender, EventArgs e)
{
    using (var httpClient = new HttpClient())
    {
        using (var request = new HttpRequestMessage(HttpMethod.Post, "https://api.baselinker.com/connector.php"))
        {
            request.Headers.TryAddWithoutValidation("X-BLToken", "AAA-BBB-CCC");

            request.Content = new StringContent("method=getOrders&parameters=%7B%22date_from%22%3A+1407341754%7D");
            request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");

            var response = await httpClient.SendAsync(request);
            var oMycustomclassname = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(Convert.ToString(response));
            labelek.Text = oMycustomclassname;
        }
    }
}

帮助

英文:

I am trying to get data from Baselinker.
But I am getting an error

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: S. Path '', line 0, position 0.'

The same in WPF

private async void Button_Clicked(object sender, EventArgs e)
        {
            using (var httpClient = new HttpClient())
            {
                using (var request = new HttpRequestMessage(new HttpMethod(&quot;POST&quot;), &quot;https://api.baselinker.com/connector.php&quot;))
                {
                    request.Headers.TryAddWithoutValidation(&quot;X-BLToken&quot;, &quot;AAA-BBB-CCC&quot;);

                    request.Content = new StringContent(&quot;method=getOrders&amp;parameters=%7B%22date_from%22%3A+1407341754%7D&quot;);
                    request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(&quot;application/x-www-form-urlencoded&quot;);

                    var response = await httpClient.SendAsync(request);
                    var oMycustomclassname = Newtonsoft.Json.JsonConvert.DeserializeObject&lt;dynamic&gt;(Convert.ToString(response));
                    labelek.Text = oMycustomclassname;
                }
            }
        }

Help

答案1

得分: 1

一个替代方案:

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("").Result;
if (response.IsSuccessStatusCode)
{
    string dataobject = response.Content.ReadAsStringAsync().Result;
    root = JsonConvert.DeserializeObject<Root>((string)dataobject);
    lista = root.Fundamentos;
} ...
英文:

An alternative:

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(&quot;application/json&quot;));
HttpResponseMessage response = client.GetAsync(&quot;&quot;).Result;
if (response.IsSuccessStatusCode)
   {
     string dataobject = response.Content.ReadAsStringAsync().Result;
     root = JsonConvert.DeserializeObject&lt;Root&gt;((string)dataobject);
     lista = root.Fundamentos;
   } ...

答案2

得分: 0

Convert.ToString(response) 不是获取HTTP响应主体作为字符串的正确方法。这可能会返回 &quot;System.Net.Http.HttpResponseMessage&quot;,显然不是JSON。

我怀疑你想要:

var response = await httpClient.SendAsync(request);
string content = await response.Content.ReadAsStringAsync();
var deserialized = JsonConvert.DeserializeObject&lt;dynamic&gt;(json);
英文:

Convert.ToString(response) isn't the right way to get the body of an HTTP response as a string. That's likely to return &quot;System.Net.Http.HttpResponseMessage&quot; which clearly isn't JSON.

I suspect you want:

var response = await httpClient.SendAsync(request);
string content = await response.Content.ReadAsStringAsync();
var deserialized = JsonConvert.DeserializeObject&lt;dynamic&gt;(json);

huangapple
  • 本文由 发表于 2023年6月29日 03:28:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定