c# - How to retrieve value from nested struct -


i creating application retrieve data facebook page using fql. fql query is:

select id, text, attachment.media.image.src comment  post_id in (select post_id stream                    source_id = "mypageid" ,  actor_id = "mypageid" limit 100) 

i got value id , text using way:

public class mycomments {     public string id { get; set; } // comment id     public string text { get; set; } // comment }  list<mycomments> q = jsonconvert.deserializeobject<list<mycomments>>                                                      (results.data.tostring()); if (q.count != 0) {     post_id = q[0].id.tostring();     post_text = q[0].text.tostring(); } 

but how value src, because struct in struct attachment > in struct media > in struct image > string src.

is there possible way value string src? in c# code?

please guys, in advance.

i haven't played fql, json easy enough deal with.

the first thing examine json object returned fql , see if has nested structure objects in or not. assuming does, need create class each nested object.

let's assume have json structure similar this:

[{     "id": "<comment id>",     "text": "<comment text>",     "attachment": {         "media": {             "image": {                 src: "<image source string>"             }         }     } }, {     ... }] 

in order unpack c# class you'll need class each level: attachment -> media -> image:

public class mycomments {     public string id { get; set; }     public string text { get; set; }     public attachment attachment { get; set; }      public class attachment     {         public media media { get; set; }     }      public class media      {         public image image { get; set; }     }      public class image      {         public string src { get; set; }     } } 

of course if have use classes outside of single response can un-nest them , add other fields might use. find simpler use way, since encapsulated within result record class.

once you've deserialized response fql should able access src item (in code above):

post_src = q[0].attachment.media.image.src; 

just careful json matches structure you've laid out it, or deserialization errors. watch arrays - can mess unless you're ready them.


edit: [removed section on arrays since doesn't fit actual use-case]


here's set of classes deserialize json posted on pastebin:

public class mycomments {     public string post_id;     public string message;     public attachment attachment;       public class attachment     {         public media[] media;     }      public class media     {         public string href;         public string alt;         public string type;         public string src;         public photo photo;     }      public class photo     {         public string aid;         public string pid;         public string fbid;         public string owner;         public int index;         public int width;         public int height;         public image[] images;     }      public class image     {         public string src;         public int width;         public int height;     } } 

tested against posted json:

mycomments comment = jsonconvert.deserializeobject<mycomments>(jsrc); console.writeline("source: {0}", comment.attachment.media[0].photo.images[0].src); 

output:

source: https://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-xfa1/t1.0-9/s720x720/10288743_852243528123890_8104585654908358176_n.jpg 

just aware arrays can empty, above fail indexoutofrangeexception. test length of arrays before indexing them.

i'd write code in mycomments.photo class find image matches criteria - largest dimensions, instance. this:

public image largestimage() {     return images.orderbydescending(i => i.width).thenbydescending(i => i.height).firstordefault(); } 

then can largest image, regardless of order images supplied in, or null if image list empty:

var img = comment.attachment.media[0].photo.largestimage(); if (img != null)     console.writeline("source: {0}", img.src); 

for list of sources of largest image media attached comment:

var q =      (         m in comment.attachment.media         let img = m.photo.largestimage()         img != null         select img.src     ).tolist(); 

or without largestimage method:

var q =      (         m in comment.attachment.media         let img = m.photo.images             .orderbydescending(i => i.width)             .thenbydescending(i => i.height)             .firstordefault()         img != null         select img.src     ).tolist(); 

works if no images entries parsed out of json whatever reason.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -