0

I was researching during a lot of time about how deserialize this json file:

{  
"clients":[  
  {  
     "id":"a0ece5db-cd14-4f21-812f-966633e7be86",
     "name":"Britney",
     "email":"[email protected]",
     "role":"admin"
  },
  {  
     "id":"e8fd159b-57c4-4d36-9bd7-a59ca13057bb",
     "name":"Manning",
     "email":"[email protected]",
     "role":"admin"
  },
  {  
     "id":"a3b8d425-2b60-4ad7-becc-bedf2ef860bd",
     "name":"Barnett",
     "email":"[email protected]",
     "role":"user"

     [ ... ]

This is my controller code:

 using (var sr = new StreamReader(response.GetResponseStream()))
{
    receivedData = sr.ReadToEnd();
}

var userList = JsonConvert.DeserializeObject<List<User>>(receivedData);

foreach (User us in userList)
{
   //this method just print the user Id
   MsgBox(u.Id);
}

So, I am absolutely stuck because the Json

JsonConvert.DeserializeObject>(receivedData); launch the following exception:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[InsuranceWebAPI.Models.User]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

I try to set the json data into an array and a list, but doesn't works. I know that maybe is a very beginner question but I am not used to work with Json and Json.net

Can any body help me to get the data into an array or List.

Thanks in advance to everybody and sorry if I don't understand properly the other questions about Json.net

2 Answers 2

1

That is because your JSON format is not of a collection of users. It is of an object containing a property named clients which is a collection of users. See this model:

public class RootObject
{
    public IEnumerable<User> clients { get; set; }
}
public class User
{
    public string id { get; set; }
    public string name { get; set; }
    public string email { get; set; }
    public string role { get; set; }
}

And by executing this:

var data = JsonConvert.DeserializeObject<RootObject>(File.ReadAllText(receivedData));
Sign up to request clarification or add additional context in comments.

9 Comments

Works! Buy I can't acces to the data as an array, maybe I asked a different thing excuseme, how can I manipulate the client data inside the data variable, to store it into a class?
@MadDev - didn't understand current problem
@MadDev - if you need clients to be an array or list, you can change it from IEnumerable<User> to List<User> or User[] and it will still work.
Forget get it @Gilad Green, Too much hours studying and working today :( you really help me man. A lot of thanks!
Thats true @Gilad Green, seems that is more convenient to use the IEnumerable, finally the code works fine, now I have problems to populate with the data a TextArea hehehe... but this is another issue... ;-) If you can check it this is the link: stackoverflow.com/questions/43698982/…
|
0

Or..

        dynamic userList = JsonConvert.DeserializeObject(File.ReadAllText("file.js"));

        foreach (var user in userList.clients)
        {
            ///MessageBox.Show(user.id);
            Console.WriteLine(user.id);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.