0

This is how my json looks like: enter image description here

I created the following code:

public class user
{
    public string username { get; set; }
    public int userid
    {
        get;
        set;
    }
    public string red
    {
        get;
        set;
    }

    public string acompaccompelted_setup
    {
        get;
        set;
    }
}

To get the data from URL I used the following code:

string url = "localhost/testingdata/file.json";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
string jsonValue = "";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)  
{
    StreamReader reader = new StreamReader(response.GetResponseStream());
    json = reader.ReadToEnd();
}

user  listing = JsonConvert.DeserializeObject<user>(json);

Console.WriteLine(listing.username);
Console.ReadLine();

Unfortunately, I'm not able to get the value for the string "username". It returns an empty value. If I try to use :

List<user> items = JsonConvert.DeserializeObject<List<user>>(json);

Then I'm getting another error. If I call the:

Console.WriteLine(json);

Then I'm getting the complete list of JSON. But I want to extract the username only. I tried to follow steps given here https://www.c-sharpcorner.com/article/working-with-json-in-C-Sharp/ but with no success. What I'm doing wrong?

1 Answer 1

2

Define a wrapper UserInfo class to represent the whole object as follows:

class UserInfo
{
    [JsonProperty(PropertyName = "user", NullValueHandling = NullValueHandling.Ignore)]
    public User User { get; set; }
}

Define the User class as follows:

class User
{
    [JsonProperty(PropertyName = "userName", NullValueHandling = NullValueHandling.Ignore)]
    public string UserName { get; set; }
    [JsonProperty(PropertyName = "userId", NullValueHandling = NullValueHandling.Ignore)]
    public long  UserId { get; set; }
    [JsonProperty(PropertyName = "accompletedSetup", NullValueHandling = NullValueHandling.Ignore)]
    public string AccompletedSetup { get; set; }
    [JsonProperty(PropertyName = "accompletedInfo", NullValueHandling = NullValueHandling.Ignore)]
    public AccompletedInfo AccompletedInfo { get; set; }
}

The accompletedInfo property is a complex object. So define a new class to represent it as follows:

class AccompletedInfo
{
}

Follow the same pattern for all the nested objects. i.e. define a class for each nested object with properties and name of the property in the JsonProperty attribute.

Then deserialize using JsonConvert as follows:

var user = JsonConvert.DeserializeObject<UserInfo>(json);

The object user now has all the properties as expected.

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

4 Comments

I tried to apply the suggestion but now I get for this line " var user = JsonConvert.DeserializeObject<UserInfo>(jsonValue); " the following error :Additional information: Could not convert string to integer: 858608032271478784. Path 'user.userid', line 1, position 135.. I tried to apply Convert.ToInt(32)
Please use bigger datatype like long for UserId property.
God! Now it works! You're a star! I spent days dealing with it! I have learned a lot from your comments!
May I ask you one more questions as this is the first time I'm dealing with JSON and reading a lot, but it seems I still miss something. The previous one works like a charm. I tried to access the complex object and I did it this way trying to follow your suggestion: public AccompletedInfo AccompletedInfo { get; set; } class AccompletedInfo { [JsonProperty(PropertyName = "completed", NullValueHandling = NullValueHandling.Ignore)] public string completed { get; set; } } var user = JsonConvert.DeserializeObject<AccompletedInfo>(json); But I get 0 value. Why?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.