Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to get the value of Email from the below json object in c#

JavaScript
{
  "data": {
    "type": "get-company-clients-response",
    "attributes": {
      "clients": [
        {
          "accountId": "53784",
          "nameOfPerson": {
            "preNominalLetters": null,
            "initials": null,
            "firstName": "bob",
            "middleName": null,
            "lastName": "gebruiker",
            "postNominalLetters": null
          },
          "email": "test@test.com",
          "profilePhoto": {
            "guid": null,
            "profilePhotoUrl": "http://www.gravatar.com",
            "useOwnProfilePhoto": false
          },
          "clientType": 0
        },
        {
          "accountId": "56308",
          "nameOfPerson": {
            "preNominalLetters": null,
            "initials": null,
            "firstName": "test",
            "middleName": null,
            "lastName": "test",
            "postNominalLetters": null
          },
          "email": "test@test.com",
          "profilePhoto": {
            "guid": null,
            "profilePhotoUrl": "http://www.gravatar.com",
            "useOwnProfilePhoto": false
          },
          "clientType": 0
        }
      ]
    }
  }
}


What I have tried:

C#
JObject results = JObject.Parse(companyClients);
                    
                    foreach (var result in results["data"])
                    {
                        companyClients.Add((string)result["attributes"]);
                    }
Posted
Updated 13-Feb-20 0:41am
Comments
Richard MacCutchan 13-Feb-20 6:07am    
What is the question?

1 solution

Create the appropriate classes:
C#
public class NameOfPerson
{
    public object preNominalLetters { get; set; }
    public object initials { get; set; }
    public string firstName { get; set; }
    public object middleName { get; set; }
    public string lastName { get; set; }
    public object postNominalLetters { get; set; }
}

public class ProfilePhoto
{
    public object guid { get; set; }
    public string profilePhotoUrl { get; set; }
    public bool useOwnProfilePhoto { get; set; }
}

public class Client
{
    public string accountId { get; set; }
    public NameOfPerson nameOfPerson { get; set; }
    public string email { get; set; }
    public ProfilePhoto profilePhoto { get; set; }
    public int clientType { get; set; }
}

public class Attributes
{
    public List<Client> clients { get; set; }
}

public class Data
{
    public string type { get; set; }
    public Attributes attributes { get; set; }
}

public class RootObject
{
    public Data data { get; set; }
}
(I used json2csharp - generate c# classes from json[^] to get those) and then just cast the result to the root class. You can then access the data in the normal way.

I prefer Newtonsoft JSON (Json.NET - Newtonsoft[^]) - it just seems a little easier to use to me.
 
Share this answer
 

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