JSON 用组合框替换文本框时遇到错误
JSON error encountered when replacing a text box with a Combo Box
我对 Visual Studio 很陌生。我正在编写一个程序,当输入员工姓名并单击“获取用户”按钮时,它将从 API 检索员工的签名(例如,如果输入员工姓名 Jane Doe,则签名“JNDO”将从 API 字符串中检索。
使用文本框输入姓名时,所需信息读取成功。但是,当我用组合框替换文本框以在用户键入时建议名称时,我收到以下错误:
Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered
while parsing value: <. Path '', line 0, position 0.' This exception
was originally thrown at this call stack:
[External Code]
TimeSheets_Try_11.Controllers.WebAPI.Getsignature(string) in WebAPI.cs
TimeSheets_Try_11.Form1.button1_Click(object, System.EventArgs) in Form1.cs
[External Code]
TimeSheets_Try_11.Program.Main() in Program.cs
通过使用断点来理解值,它表示
String is not JSON formatted <!doctype html>
我的JSON字符串:
[{"signature":"JNDO","firstName":"Jane","fullName":"Doe, Jane","lastName":"Doe"}]
我的 windows 表格代码:
namespace TimeSheets_Try_11
{
public partial class Form1 : Form
{
WebAPI WA = new WebAPI();
public Form1()
{
InitializeComponent();
webBrowser1.Url = new Uri(StaticStrings.UrlIora);
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.DataSource = WA.Getsignature(textBox2.Text);
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(AutoCompleteStringCollection combData)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
Form2 p = new Form2();
p.ShowDialog();
}
}
}
调用我的代码 JSON
namespace TimeSheets_Try_11.Controllers
{
class WebAPI
{
public string Getsignature(string name)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var cookies = FullWebBrowserCookie.GetCookieInternal(new Uri(StaticStrings.UrlIora), false);
WebClient wc = new WebClient();
wc.Encoding = System.Text.Encoding.UTF8;
wc.Headers.Add("Cookie:" + cookies);
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
wc.UseDefaultCredentials = true;
string uri = "";
uri = StaticStrings.UrlIora + name;
var myJsonResponse = wc.DownloadString(uri);
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
string signame = myDeserializedClass.ToString();
return signame;
}
}
定义我的变量的代码JSON:
namespace TimeSheet_Try11_Models
{
public class Employeename
{
public string signature { get; set; }
public string firstName { get; set; }
public string fullName { get; set; }
public string lastName { get; set; }
}
public class Root
{
public List<Employeename> Employeename { get; set; }
}
}
我个人不会将文本框换成组合来提供自动完成功能,我会将文本框的 AutoCompleteMode
设置为例如SuggestAppend
并将其 AutoCompleteSource
设置为例如CustomSource
然后将它的 AutoCompleteCustomSource
设置为一个 AutoCompleteStringCollection
由类似于你的 API 的东西构建,响应所有名字的列表
var acsc = new AutoCompleteStringCollection();
acsc.Add("Jane Doe"); // value from API perhaps?
acsc.Add("John Smith"); // value from API query all
当用户让texbox帮他们写好名字后,可以将名字提交给API加载其余的用户信息
一个组合可以提前工作;下载所有数据,选择其中的一些显示在组合中,其余的保留在后面的代码中访问..
我对 Visual Studio 很陌生。我正在编写一个程序,当输入员工姓名并单击“获取用户”按钮时,它将从 API 检索员工的签名(例如,如果输入员工姓名 Jane Doe,则签名“JNDO”将从 API 字符串中检索。
使用文本框输入姓名时,所需信息读取成功。但是,当我用组合框替换文本框以在用户键入时建议名称时,我收到以下错误:
Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: <. Path '', line 0, position 0.' This exception was originally thrown at this call stack: [External Code] TimeSheets_Try_11.Controllers.WebAPI.Getsignature(string) in WebAPI.cs TimeSheets_Try_11.Form1.button1_Click(object, System.EventArgs) in Form1.cs [External Code] TimeSheets_Try_11.Program.Main() in Program.cs
通过使用断点来理解值,它表示
String is not JSON formatted <!doctype html>
我的JSON字符串:
[{"signature":"JNDO","firstName":"Jane","fullName":"Doe, Jane","lastName":"Doe"}]
我的 windows 表格代码:
namespace TimeSheets_Try_11
{
public partial class Form1 : Form
{
WebAPI WA = new WebAPI();
public Form1()
{
InitializeComponent();
webBrowser1.Url = new Uri(StaticStrings.UrlIora);
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.DataSource = WA.Getsignature(textBox2.Text);
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(AutoCompleteStringCollection combData)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
Form2 p = new Form2();
p.ShowDialog();
}
}
}
调用我的代码 JSON
namespace TimeSheets_Try_11.Controllers
{
class WebAPI
{
public string Getsignature(string name)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var cookies = FullWebBrowserCookie.GetCookieInternal(new Uri(StaticStrings.UrlIora), false);
WebClient wc = new WebClient();
wc.Encoding = System.Text.Encoding.UTF8;
wc.Headers.Add("Cookie:" + cookies);
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
wc.UseDefaultCredentials = true;
string uri = "";
uri = StaticStrings.UrlIora + name;
var myJsonResponse = wc.DownloadString(uri);
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
string signame = myDeserializedClass.ToString();
return signame;
}
}
定义我的变量的代码JSON:
namespace TimeSheet_Try11_Models
{
public class Employeename
{
public string signature { get; set; }
public string firstName { get; set; }
public string fullName { get; set; }
public string lastName { get; set; }
}
public class Root
{
public List<Employeename> Employeename { get; set; }
}
}
我个人不会将文本框换成组合来提供自动完成功能,我会将文本框的 AutoCompleteMode
设置为例如SuggestAppend
并将其 AutoCompleteSource
设置为例如CustomSource
然后将它的 AutoCompleteCustomSource
设置为一个 AutoCompleteStringCollection
由类似于你的 API 的东西构建,响应所有名字的列表
var acsc = new AutoCompleteStringCollection();
acsc.Add("Jane Doe"); // value from API perhaps?
acsc.Add("John Smith"); // value from API query all
当用户让texbox帮他们写好名字后,可以将名字提交给API加载其余的用户信息
一个组合可以提前工作;下载所有数据,选择其中的一些显示在组合中,其余的保留在后面的代码中访问..