Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1037967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:54:57+00:00 2026-05-16T14:54:57+00:00

I have a JSON multi-level response that I need to deserialize and from the

  • 0

I have a JSON “multi-level” response that I need to deserialize and from the deserialized classes structure I need to extract all the objects of a certain class.

Below the code I’m using, at the end I find that my result is empty, not populated.

// given these two classes:
[DataContract]
public class ThingsList
{
    [DataMember(Name = "status")]
    public string Status { get; set; }
    [DataMember(Name = "since")]
    public double Since { get; set; }
    [DataMember(Name = "list")]
    public Dictionary<string, ThingsListItem> Items { get; set; }
    public DateTime SinceDate { get { return UnixTime.ToDateTime(Since); } }
}
[DataContract]
public class ThingsListItem
{
    [DataMember(Name = "url")]
    public string Url { get; set; }
    [DataMember(Name = "title")]
    public string Title { get; set; }
}    

// I can deserialize my json to this structure with:
ThingsList results = JsonConvert.DeserializeObject<ThingsList>(e.Result);

// now I need to "extract" only the ThingsListItem objects, and I'm trying this:
var theList = from item in results.Items.OfType<ThingsListItem>()
        select new 
                    {
                        Title = item.Title,
                        Url = item.Url
                    };
// but "theList" is not populated.

The points here are (I believe):
– I try to use results.Items.OfType() in order to extract only the ThingsListItem objects, that in the “upper” class are declared in the
public Dictionary Items { get; set; }
row.

Any idea? Tell if it’s not clear…

Thanks
Andrea

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:54:57+00:00Added an answer on May 16, 2026 at 2:54 pm

    EDIT: updated my response for clarity.

    Since your Dictionary values are of type ThingsListItem you can access them directly by using the Dictionary’s Values property. There is no need to use OfType to check their type and extract them. Simply use:

    var items = results.Items.Values;
    

    The Values property would return an ICollection<ThingsListItem>. You can then iterate over the results with a foreach. LINQ does not have to be used.


    While the Values property described above should be sufficient, I will point out a few issues with your original LINQ query attempt.

    1) The following query is probably what you were after. Again, the Dictionary’s Values property is key (no pun intended) to accessing the items:

    var theList = from item in results.Items.Values
                  select new
                  {
                      Title = item.Title,
                      Url = item.Url
                  };
    

    2) Why are you using new? That will return an IEnumerable of anonymous types. You already have a defined class, so why project into a new anonymous type? You should retain the underlying ThingsListItem items by selecting the item directly to get an IEnumerable<ThingsListItem>:

    var theList = from item in results.Items.Values
                  select item;
    foreach (var item in theList)
    {
        Console.WriteLine("Title: {0}, Url: {1}", item.Title, item.Url);
    }
    

    You would usually project into a new anonymous type to define a type with data properties you are interested in. Generally you would use them immediately after the query, whereas a selection into an existing class could be used immediately or passed around to other methods that are expecting that type.

    Hopefully this has cleared up some questions for you and you have a better idea of using LINQ and when to use the new keyword. To reiterate, for your purposes it seems the Values property should suffice. Using LINQ to select the item is redundant when there are other immediate means to do so.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have JSON string that has nested objects with dynamic names that vary each
I have JSON that keep changing , I need in android to keep updating
I have a JSON file that I would like to create an multi-dimensional array
I have Json response from my database . I am using Php for this
When you have a multi-tiered object like a json object that say has 3
I have a multi-dimensional associative array that is encoded into JSON for database storage,
I am attempting to pull down data from a JSON response that I wrote
I have JSON text that looks like this: { ok: true, totalPages: 256, arReports:
I have a JSON feed that is pulling into an each statement. What I
I want to create a multi-level JSON string with JS. Scenario 3 countries with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.