0

I would like to start by saying that I am not a developer and this is my very first time writing a code to this extend of complication (at least to me). Any help/guidance would be much appreciated.

The idea of this program is to retrieve the employee user ID (or signature) from an API URL once the name has been entered.

I have a JSON String

[{"signature":"JDOW","firstName":"Jane","fullName":"Dow, Jane","lastName":"Dow"}]

I am trying to deserialize it to a collection. But I am getting an error. Can somebody direct me to the right way to fix this?

namespace TimeSheet_Try11_Models
{
    public class Employeename
    {
        [JsonProperty("Signature")]
        public string Signature { get; set; }

        [JsonProperty("FirstName")]
        public string FirstName { get; set; }
        
        [JsonProperty("FullName")]
        public string FullName { get; set; }
       
        [JsonProperty("LastName")]
        public string LastName { get; set; }
    } 
}

I a trying to convert using the following code:

uri = StaticStrings.UrlIora + name;
var response = wc.DownloadString(uri);

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

The error I am getting is:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'TimeSheet_Try11_Models.Employeename' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

5
  • 6
    Yo're currently not trying to deserialize it to a collection - you're trying to deserialize it to a single Employeename. If you call DeserializeObject<List<Employeename>> then it'll try to deserialize it to a collection - and I suspect all will be well. Commented Aug 24, 2020 at 19:08
  • Thank you for your comment, however, I am not getting a red line under the whole statement : JsonConvert.DeserializeObject<List<Employeename>>(response); saying that "cannot implicitly convert type 'System.collection.Generic.List<TimeSheet_Try11_Models.Employeename>' to 'TimeSheet_Try11_Models.Employeename'" Commented Aug 24, 2020 at 19:16
  • 1
    Well no, you need to change the type of your variable as well, because you're trying to deserialize to a collection. (Why you'd call that variable status when it's a collection of employee names is another matter...) Commented Aug 24, 2020 at 19:17
  • That fixed the error, thank you. I have another silly question (once again this is my first time programming). I originally had the statement string signature = status.Signature.ToString(); and the return signature. Now, there is a redline under "status" of course so what would I change it to? Commented Aug 24, 2020 at 19:27
  • Stack Overflow is not intended to be a back-and-forth - and this isn't a productive way to learn programming. If you don't know the basics, you will be constantly fighting for every single line of code. I strongly advise you to start with a beginner's tutorial on C#, otherwise your progress will be very slow indeed. Commented Aug 24, 2020 at 19:32

2 Answers 2

1

Deserialize the Json data for collection as:

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

or

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

Another thing is that, no need to use attribute [JsonProperty("anyProperty")] with your properties. Json can be deserialized without using it in attributes.

After getting the deserialzed data in status object, any value can be fetched from that object as:

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

Similarly other values can also be taken from the status.

Sign up to request clarification or add additional context in comments.

Comments

1

Your JSON string has square brackets ([]) around it, which means it is a collection of items. This is why the Deserialiser is erroring.

You need to either remove those brackets to give just one employee object in the input string, or tell DeserializeObject that it is a List you are deserialising.

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

Working Fiddle here

2 Comments

That fixed the error, thank you. I have another silly question (once again this is my first time programming). I originally had the statement string signature = status.Signature.ToString(); and the return signature. Now, there is a redline under "status" of course so what would I change it to?
See in the fiddle - because the output from the deserialisation is a List, you need to get an item from it, by indexing with [0] before using .Signature

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.