3

I have an example of what I'm trying to deserialize below and I'm getting a bunch of different errors when I try to pull the data. All I want is to pull the id value but I can't seem to figure it out.

{"object":"payments","entry":[{"id":"546787862118679","time":1417135022,"changed_fields":["actions"]}]}

    public class Entry
    {
        public string id { get; set; }
        public int time { get; set; }
        public List<string> changed_fields { get; set; }
    }

    public class RootObject
    {
        public string @object { get; set; }
        public List<Entry> entry { get; set; }
    }

    dynamic result = new StreamReader(request.InputStream).ReadToEnd();
    var items = JsonConvert.DeserializeObject<RootObject>(result);
    string paymentID = items.entry.FirstorDefault().id;

Returns this error:

'System.Collections.Generic.List' does not contain a definition for 'FirstOrDefault()'

2
  • 1
    try adding using System.Linq; to top of the file. Commented Nov 28, 2014 at 3:17
  • @ErikPhilips It is already there Commented Nov 28, 2014 at 3:17

6 Answers 6

3

I have a feeling it would work if your code:

string paymentID = items.entry.FirstorDefault().id;

were

string paymentID = items.entry.FirstOrDefault().id;

And that your error is actually

'System.Collections.Generic.List' does not contain a definition for 'FirstorDefault'

As Collections.Generic.List does contain a definition for FirstOrDefault (System.Linq)

Sign up to request clarification or add additional context in comments.

Comments

2

try this

string paymentID = (items.entry.FirstOrDefault()).id;

Get the first item from list entry and then get idfrom that item.

3 Comments

I'm getting this error when I try this: 'System.Collections.Generic.List<HomeworkHelpers.RTUSubscriptionActionFilter.Entry>' does not contain a definition for 'FirstorDefault'
see this FirstorDefault() works with collection. this entry is a list or collection right ?
@RohitGupta there's a typo in your answer, it should be FirstOrDefault (capital O)
1

From a quick look at the error you get, it occurs on this line:

string paymentID = items.entry.FirstorDefault().id; //should be .FirstOrDefault().id;

...to enumerate through each Entry item in items.entry which is a List<Entry>:

if (items.entry != null && items.entry.Count > 0)
{
    foreach(Entry entryItem in items.entry)
    {
        if (!string.IsNullOrEmpty(entryItem.id))
        {
            string paymentID = entryItem.id;
        }
    }
}

...to only access the first Entry item:

if (items.entry != null && items.entry.Count > 0)
{        
    if (!string.IsNullOrEmpty(items.entry[0].id))
    {
        string paymentID = items.entry[0].id;
    }

}

6 Comments

I'm getting this error: 'System.Collections.Generic.List<HomeworkHelpers.RTUSubscriptionActionFilter.Entry>' does not contain a definition for 'id'
see my updated answer. you're trying to access List<Entry>.id which doesn't make sense, you need to access the id property of each individual Entry object in List<Entry> instead
the json string is always the exact same. There is only one entry object so still use the same code?
yes, if there's only one Entry object then List<Entry> will only contain one item so the foreach loop will only execute once
@user3610374 please consider removing the downvote from my answer as well
|
1

Why are you loading the result into a dynamic type??? Use a string.

void Main()
{
    string json = @"{""object"":""payments"",""entry"":[{""id"":""546787862118679"",""time"":1417135022,""changed_fields"":[""actions""]}]}";

    Root root = JsonConvert.DeserializeObject<Root>(json);

    Entry entry = root.Entry.FirstOrDefault();
}


public class Entry
{

    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("time")]
    public int Time { get; set; }

    [JsonProperty("changed_fields")]
    public string[] ChangedFields { get; set; }
}

public class Root
{

    [JsonProperty("object")]
    public string Object { get; set; }

    [JsonProperty("entry")]
    public Entry[] Entry { get; set; }
}

1 Comment

I'm getting this error when I try this: Value cannot be null. Parameter name: source
1

For some reason (I still don't know why), your code works when you change the type of result from dynamic to string.

string result = new StreamReader(request.InputStream).ReadToEnd();
if (!string.IsNullOrWhiteSpace(result)) {
    var items = JsonConvert.DeserializeObject<RootObject>(result);
    string paymentID = items.entry.FirstorDefault().id;
}

1 Comment

FirstorDefault should be FirstOrDefault else you'll get System.Collections.Generic.List<Ent‌​ry>' does not contain a definition for 'FirstorDefault'
0

Do it like this

string paymentID = (items.entry as List<Entry>).FirstOrDefault().id;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.