英文:
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¶meters=%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("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;
}
}
}
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("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;
} ...
答案2
得分: 0
Convert.ToString(response)
不是获取HTTP响应主体作为字符串的正确方法。这可能会返回 "System.Net.Http.HttpResponseMessage"
,显然不是JSON。
我怀疑你想要:
var response = await httpClient.SendAsync(request);
string content = await response.Content.ReadAsStringAsync();
var deserialized = JsonConvert.DeserializeObject<dynamic>(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 "System.Net.Http.HttpResponseMessage"
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<dynamic>(json);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论