Link to home
Start Free TrialLog in
Avatar of Seven price
Seven priceFlag for United States of America

asked on

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

Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

foreach (BelongsTo belongs_to in query)
                    {
                   // do something . . . query is a list of Belongs_to
                    }
Avatar of Seven price

ASKER

Error      1      Cannot convert type 'AnonymousType#1' to BelongsTo'      

Getting this error
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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

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();