Click here to Skip to main content
14,869,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am very new to visual studio. I am writing a program which will retrieve the signature of an employee from an API when their name is entered and a "Get user" button is clicked (e.g. if the employee name Jane Doe is entered, the signature "JDOE" will be retrieved from the API string when the name is entered and the button is clicked).

I have been able to read the data successfully with a text box, however, I would like to incorporate a combo box so that the combo box suggests names as the user is typing (similar to google).

When I try to run my code with a combo box, the following is the error I get for my JSON:
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]


What I have tried:

JSON String:
[{"signature":"JNDO","firstName":"Jane","fullName":"Doe, Jane","lastName":"Doe"}]


Code for my windows form:
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 webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs 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(); 
        }
    }
}


Code calling out the 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 response = wc.DownloadString(uri);

            var status = JsonConvert.DeserializeObject<List<Employeename>>(response);

            string signame = status.Select(js => js.signature).First();
            return signame;

        }
}


Code defining the variables:
namespace TimeSheet_Try11_Models
{

        // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
        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; }
       
        }
    
    

    }
Posted
Updated 8-Oct-20 7:37am

1 solution

You need to serialize / deserialize the "root" class; you (normally) can't serialize / deserialize just a list.

var status = JsonConvert.DeserializeObject<List<Employeename>>(response);


(It says so further on in the program comments).
   
v2
Comments
programmer1010101 8-Oct-20 14:19pm
   
Thank you very much for your comment. I did change that accordingly, however, I am still getting the same error.
Sandeep Mewara 8-Oct-20 15:12pm
   
What did you change to?

What Gerry suggested is to have:
var status = JsonConvert.DeserializeObject<Root>(response);

Don't think you should get same error as earlier with above. Share the new code and error.
programmer1010101 8-Oct-20 15:25pm
   
Yes. The following is the new code I have revised:

uri = StaticStrings.UrlIora + name;

var myJsonResponse = wc.DownloadString(uri);
Root myDeserializedClass = JsonConvert.DeserializeObject<root>(myJsonResponse);

string signame = myDeserializedClass.ToString();
return signame;
However, I am still receiving the same error.
Sandeep Mewara 8-Oct-20 16:04pm
   
What is the value of response when you debug?
programmer1010101 8-Oct-20 16:13pm
   
It should retrieve the employee signature based on the name entered in the combo box, if this is what you are referring to when asking for a value. The combo box should suggest names as it is being entered (I'm sorry if I didn't answer the question correctly, I am very new to the programing referrals).
Richard Deeming 9-Oct-20 3:34am
   
"It should retrieve ..." is not the actual value of the response.

Debug your code and inspect the data it actually does retrieve. It's almost certainly not the JSON you're expecting.
programmer1010101 9-Oct-20 11:25am
   
Thank you for the explanation. You are right. I get the following value (with the new json code):
"String is not JSON formatted.





<title>IORA Central
<base href="/" />



<app-root>Loading...

"

However, when I used the text box instead of a combo box along with the original JSON code, I would get the correct value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900