0

I tried to get the id Field

dynamic post = fb.Get("/" + radListControl2.SelectedItem + "/feed?fields=message");

int count = (int)post.data.Count;

for (int i = 0; i < count; i++)
{
    radListControl1.Items.Add(Convert.ToString(post.data[i].id));
}

It work and return this:

{
  "data": [
    {
      "id": "5xxx269_10151xxx50270", 
      "created_time": "2013-05-24T12:52:24+0000"
    }, 
    {
      "id": "5xxx69_101515xxxx270", 
      "created_time": "2013-05-21T19:57:57+0000"
    }, 
    {
      "message": "xxxxx", 
      "id": "xxx_1015157xxx270", 
      "created_time": "2013-05-21T19:44:07+0000"
    }, 
    {
      "id": "xxx", 
      "created_time": "2013-05-21T19:28:32+0000"
    }, 
    {
      "id": "5xx69_1015xxx75270", 
      "created_time": "2013-05-19T22:02:24+0000"
    }, 
    {
      "id": "52xxx269_1xxxx40270", 
      "created_time": "2013-05-18T09:31:42+0000"
    }, 
    {
      "message": "xxxx", 
      "id": "xxxx", 
      "created_time": "2013-05-17T22:59:49+0000"
    }, 

id xxx is inserted in radlistbox, but this code returns the same list

int count = (int)post.data.Count;

for (int i = 0; i < count; i++)
{
    radListControl1.Items.Add(Convert.ToString(post.data[i].message));
}

but radlistbox is empty, i need only field Message can you help me please?

Chris Moutray
  • 18,029
  • 7
  • 45
  • 66
Federal09
  • 639
  • 4
  • 9
  • 25
  • 1
    The question title is about `foreach` loops but the question body is about `for` loops. Which is it? – Eric Lippert May 24 '13 at 13:57
  • 1
    Is an exception thrown when you try to get `message` but none exists? – Tim S. May 24 '13 at 13:58
  • 1
    Only two of your entries have a `message` field. Is it choking on that? You might want to [test for the existence of the property](http://stackoverflow.com/questions/2998954/dynamic-how-to-test-if-a-property-is-available) before trying to use it. – Matt Burland May 24 '13 at 13:58
  • It can take the message field only when there is? – Federal09 May 24 '13 at 14:00
  • 1
    Misleading title contributes negative value to this site. Future visitors with the same problem will not be able to find this question, and people with questions about foreach will get this question in their search results even though it is of no use to them. – Raymond Chen May 24 '13 at 14:04
  • sorry, title corrected by chris =) – Federal09 May 24 '13 at 14:08

2 Answers2

1

Perhaps only add when there is a value...

for (int i = 0; i < count; i++)
{
    string message = post.data[i].message;

    if (string.IsNullOrEmpty(message)) 
        continue;

    radListControl1.Items.Add(Convert.ToString(message));
}
Chris Moutray
  • 18,029
  • 7
  • 45
  • 66
0

You can use hasOwnProperty to check for the existence of a property on an object

if (post.data[i].hasOwnProperty("message") {
    radListControl1.Items.Add(Convert.ToString(post.data[i].message));
}
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405