retrive json object

I already deserailized the json file.
they part I am having difficult Is the heiarchy.  Need to go in the BelongsTo class and get the object name value.

 public class BelongsTo
        {
       
            public List<Name> names { get; set; }
            public object age_range { get; set; }
            public object gender { get; set; }
            public string best_name { get; set; }
        
        }       


 public class Result
        {
            
            public List<BelongsTo> belongs_to { get; set; }
            
        }

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

Open in new window

 var query = (from c in des.results
                                select new {c.belongs_to}).ToArray();

                    foreach (var mylist in query)
                    {
                        mylist.belongs_to
                    }

Open in new window

LVL 9
Seven priceFull StackAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Kyle AbrahamsSenior .Net DeveloperCommented:
foreach (BelongsTo belongs_to in query)
                    {
                   // do something . . . query is a list of Belongs_to
                    }
0
Seven priceFull StackAuthor Commented:
Error      1      Cannot convert type 'AnonymousType#1' to BelongsTo'      

Getting this error
0
Kyle AbrahamsSenior .Net DeveloperCommented:
You can do this two ways . . .the anonymous way:
foreach (var belongs_to in query)
                    {
                   // do something . . . query is a list of Belongs_to
                  //use the properties, eg:                            belongs_to.names  
                    }

Open in new window


OR

Convert the object:
List<BelongsTo> query = (from c in des.results
                                select new BelongsTo {
                                 //map the properties
                                 names = c.names,
                                 age_range = c.age_range 
                               //etc
                                 }
).ToArray();

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
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Seven priceFull StackAuthor Commented:
Sorry, for example. if you look at my property classes.

I cannot pull c.names for example.
when I try this below. I get.
List<BelongsTo> query = (from c in des.results
                                select new BelongsTo {
                                 //map the properties
                                 names = c.names,
                                 age_range = c.age_range 
                               //etc
                                 }
).ToArray();

Open in new window


List<BelongsTo> query = (from c in des.results
                                select new BelongsTo {
                                 //map the properties
                                 names = c.belongs_to,
                             
                               //etc
                                 }
).ToArray();

Open in new window

0
Seven priceFull StackAuthor Commented:
 public class Result
        {
            
            public List<BelongsTo> belongs_to { get; set; }
            
        }

        public class RootObject
        {

Open in new window


I do not thing  you see my structure of my classes. other wise it would be that easy.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
Belongs To is it's own class though.

What is the data in des.results?

If des.Results it's a list of BelongsTo (eg: your results class) then the from operator should enumerate over the table.  

Look at example 5:
http://www.codeproject.com/Articles/26743/Using-LINQ-to-Objects-in-C

when you do a from you automatically get the class.

Why is it treating Des.Results as anonymous?
0
Seven priceFull StackAuthor Commented:
But the way the json data is coming in. at first it comes from the rootobject. The the root object is call the class result in which it has BelongsTo as a list that calls the BelongsTo class. But I have to call the root first because the data always return null if I return the BelongsTo as its own class.  i don't know. I see if I can figure it out. I know it suppose to work and if i was just calling the Result class i get the data. But the parent to a parent to a child is killing me.
0
Seven priceFull StackAuthor Commented:
ok the reason it is returning anonymous is because again. you guys are not looking at my class.
I am going to break it down for you.
########################### here I am starting my select using my Json file deserializing with Root object.
  Part 1
 var des = JsonConvert.DeserializeObject<RootObject>(json);
   public class RootObject
        {
            public List<Result> results { get; set; }
           
        }

Open in new window


###################  Here the root object is pulling my result set where I have the class BelongsTo as a list that is pulling belongs_to . This is where it keep stopping at. So I get anonymous because I need to select in the linq statement a single property. I cannot select a single property because it does not go to the next level.

##############
Part 2
public class Result
        {
            
            public List<BelongsTo> belongs_to { get; set; }
            
        }

Open in new window


Part 3 the result set I need to get.
 public class BelongsTo
        {
       
            public List<Name> names { get; set; }
            public object age_range { get; set; }
            public object gender { get; set; }
            public string best_name { get; set; }
        
        }       

Open in new window

0
Seven priceFull StackAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for sevensnake77's comment #a40758798
Assisted answer: 250 points for Kyle Abrahams's comment #a40758466
Assisted answer: 0 points for sevensnake77's comment #a40758499
Assisted answer: 250 points for Kyle Abrahams's comment #a40758694

for the following reason:

var query = (from c in des.results
                             from s in c.belongs_to
                             from k in s.best_name
                             select new {s.best_name }).Distinct();
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.