Click here to Skip to main content
16,018,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am working with uwp i can't able to find the what mistake i have done in the program when i debug the code is running prety well but i am getting exception error . the error i got i have written in // (error)

What I have tried:

string json = JsonConvert.SerializeObject(new
         {
             UN = UN,
             Pwd = Pwd,
             DTP = 1,
             OST = 1
         });
         WebClient wc = new WebClient();
         wc.Headers["Content-Type"] = "application/json";
         try
         {
             string response = wc.UploadString(endpoint, method, json);
             return JsonConvert.DeserializeObject<User>(response);
            // i got the username and password in response  the code is running correct
         }

         catch (Exception ex)
         //Exception error {"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Windows.System.User' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\nPath '', line 1, position 1."}
         {
             return null;
         }
Posted
Updated 6-Nov-22 19:26pm
v2

1 solution

Read the error message: it's pretty explicit.
Your JSON does not match the class you are trying to deserialize into.

So use the debugger to look at exactly what the JSON in response contains, and if necessary use an online class constructor ( I use Convert JSON to C# Classes Online - Json2CSharp Toolkit[^] myself) to construct the classes in the data, then compare them to the user class you are expecting.

Sorry, but we can't do any of that for you!
 
Share this answer
 
Comments
Ailiseu Brigitta 7-Nov-22 1:34am    
no need sorry for that OriginalGriff thankyou for your response
OriginalGriff 7-Nov-22 2:03am    
You're welcome!

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