read Json to an object c#

trying to read json file but does not return as list or Array or just get the value of the name. json2.txt

 var model = new LooupInfo
                             {
                                 gender = (o["gender"]).ToString(),
                                 firstname = (o["first_name"]).ToString(),
                                 middlename = (o["middle_name"]).ToString(),

}

public partial class LooupInfo
    {
        public string gender{ get; set; }
        public string middlename { get; set; }
        public string firstname { get; set; }
}
   


 JObject o1 = JObject.Parse(File.ReadAllText(@"json2.json"));
               string rowsAdded = Convert.ToString((string)o1["results"][0]["first_name"]);
               Console.WriteLine(rowsAdded);
                string allText = System.IO.File.ReadAllText(
        @"json2.json");
                   
                using (StreamReader r = new StreamReader(@"json2.json"))
                {
                    string json = r.ReadToEnd();
                    var des = (List<LooupInfo>)JsonConvert.DeserializeObject(json, json.GetType());
                  //  List<LooupInfo> items = JsonConvert.DeserializeObject<List<LooupInfo>>(json);
                }

Open in new window

LVL 9
Seven priceFull StackAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Craig WagnerSoftware ArchitectCommented:
Using http://json2csharp.com we find that the classes to deserialize that JSON are actually represented by the following:

public class Id
{
    public string key { get; set; }
    public string url { get; set; }
    public string type { get; set; }
    public string uuid { get; set; }
    public string durability { get; set; }
}

public class Id2
{
    public string key { get; set; }
    public object url { get; set; }
    public string type { get; set; }
    public string uuid { get; set; }
    public string durability { get; set; }
}

public class Name
{
    public object salutation { get; set; }
    public string first_name { get; set; }
    public object middle_name { get; set; }
    public string last_name { get; set; }
    public object suffix { get; set; }
    public object valid_for { get; set; }
}

public class Id3
{
    public string key { get; set; }
    public string url { get; set; }
    public string type { get; set; }
    public string uuid { get; set; }
    public string durability { get; set; }
}

public class Reputation
{
    public int spam_score { get; set; }
    public int spam_index { get; set; }
}

public class Phone
{
    public Id3 id { get; set; }
    public string line_type { get; set; }
    public object belongs_to { get; set; }
    public object associated_locations { get; set; }
    public bool is_valid { get; set; }
    public string phone_number { get; set; }
    public string country_calling_code { get; set; }
    public object extension { get; set; }
    public string carrier { get; set; }
    public bool do_not_call { get; set; }
    public Reputation reputation { get; set; }
    public object is_prepaid { get; set; }
    public object best_location { get; set; }
    public object contact_type { get; set; }
}

public class BelongsTo
{
    public Id2 id { get; set; }
    public string type { get; set; }
    public List<Name> names { get; set; }
    public object age_range { get; set; }
    public object gender { get; set; }
    public List<object> locations { get; set; }
    public List<Phone> phones { get; set; }
    public string best_name { get; set; }
    public object valid_for { get; set; }
    public bool is_historical { get; set; }
}

public class Id4
{
    public string key { get; set; }
    public string url { get; set; }
    public string type { get; set; }
    public string uuid { get; set; }
    public string durability { get; set; }
}

public class LatLong
{
    public double latitude { get; set; }
    public double longitude { get; set; }
    public string accuracy { get; set; }
}

public class AssociatedLocation
{
    public Id4 id { get; set; }
    public string type { get; set; }
    public object legal_entities_at { get; set; }
    public string address { get; set; }
    public object is_receiving_mail { get; set; }
    public object not_receiving_mail_reason { get; set; }
    public object usage { get; set; }
    public object delivery_point { get; set; }
    public object box_type { get; set; }
    public object address_type { get; set; }
    public LatLong lat_long { get; set; }
    public object is_deliverable { get; set; }
    public string standard_address_line1 { get; set; }
    public string standard_address_line2 { get; set; }
    public string standard_address_location { get; set; }
    public object contact_type { get; set; }
}

public class Reputation2
{
    public int spam_score { get; set; }
    public int spam_index { get; set; }
}

public class Id5
{
    public string key { get; set; }
    public string url { get; set; }
    public string type { get; set; }
    public string uuid { get; set; }
    public string durability { get; set; }
}

public class LatLong2
{
    public double latitude { get; set; }
    public double longitude { get; set; }
    public string accuracy { get; set; }
}

public class BestLocation
{
    public Id5 id { get; set; }
    public string type { get; set; }
    public object legal_entities_at { get; set; }
    public string address { get; set; }
    public object is_receiving_mail { get; set; }
    public object not_receiving_mail_reason { get; set; }
    public object usage { get; set; }
    public object delivery_point { get; set; }
    public object box_type { get; set; }
    public object address_type { get; set; }
    public LatLong2 lat_long { get; set; }
    public object is_deliverable { get; set; }
    public string standard_address_line1 { get; set; }
    public string standard_address_line2 { get; set; }
    public string standard_address_location { get; set; }
}

public class Result
{
    public Id id { get; set; }
    public string line_type { get; set; }
    public List<BelongsTo> belongs_to { get; set; }
    public List<AssociatedLocation> associated_locations { get; set; }
    public bool is_valid { get; set; }
    public string phone_number { get; set; }
    public string country_calling_code { get; set; }
    public object extension { get; set; }
    public string carrier { get; set; }
    public bool do_not_call { get; set; }
    public Reputation2 reputation { get; set; }
    public object is_prepaid { get; set; }
    public BestLocation best_location { get; set; }
}

public class RootObject
{
    public List<Result> results { get; set; }
    public List<object> messages { get; set; }
}

Open in new window


Once you've got those classes defined you should be able to deserialize using JSON.NET.

var des = JsonConvert.DeserializeObject<RootObject>(json);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Seven priceFull StackAuthor Commented:
Thanks. do you know the correct syntax so I can capture each value.?
0
Craig WagnerSoftware ArchitectCommented:
It's an object with properties. The correct syntax is to use dot notation.

foreach(var result in des.results)
{
    result.phone_number...
}

Open in new window


Have you never accessed the properties of an object before?
0
Seven priceFull StackAuthor Commented:
Thanks you got me much further.
0
Seven priceFull StackAuthor Commented:
one last question ok I can get result.phone_number but I need to go one more level. Its like parent parent then child.

result.belongs_to.names.
If you know, how can i get to the third level.
 public class Result
        {
        
            public List<BelongsTo> belongs_to { get; set; }
}
public class BelongsTo
        {
          
            public string type { get; set; }
            public List<Name> names { get; set; }

        }

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.