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 8912169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:07:53+00:00 2026-06-15T04:07:53+00:00

I’m in .NET for WinRT (C#) and I’d like to deserialize a JSON string

  • 0

I’m in .NET for WinRT (C#) and I’d like to deserialize a JSON string to a Dictionary<string, object>, where the dictionary value can later be converted to the actual type.
The JSON string can contain an object hierarchy and I’d like to have child objects in Dictionary<string, object> as well.

Here’s a sample JSON it should be able to handle:

{
  "Name":"John Smith",
  "Age":42,
  "Parent":
  {
    "Name":"Brian Smith",
    "Age":65,
    "Parent":
    {
       "Name":"James Smith",
       "Age":87,
    }
  }
}

I tried with the DataContractJsonSerializer doing as so:

using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
    DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings();
    settings.UseSimpleDictionaryFormat = true;

    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Dictionary<string, object>), settings);
    Dictionary<string, object> results = (Dictionary<string, object>)serializer.ReadObject(ms);
}

This actually works fine for the first level, but then “Parent” is just an object which cannot be casted to a Dictionary<string, object>:

Dictionary<string, object> parent = (Dictionary<string, object>)results["Parent"];
Cannot cast 'results["Parent"]' (which has an actual type of 'object') to 'System.Collections.Generic.Dictionary<string,object>'

I then tried using Json.NET but child objects are JObject themselves being IDictionary<string, JToken>, which forces me to iterate through the full hierarchy and convert them over again.

Would someone know how to solve this problem using an existing serializer?

EDIT

I’m using Dictionary<string, object> because my objects vary from one server call to another (e.g. the “Id” property might be “id”, *”cust_id”* or “customerId” depending on the request) and as my app isn’t the only app using those services, I can’t change that, at least for now.

Therefore, I found it inconvenient to use DataContractAttribute and DataMemberAttribute in this situation.
Instead I’d like to store everything in a generic dictionary, and have a single strongly-typed property “Id” which looks for “id”, “cust_id” or “customerId” in the dictionary making it transparent for the UI.

This system works great with JSON.NET, however if ever the server returns an object hierarchy, sub-objects will be stored as JObjects in my dictionary instead of another dictionary.

To sum-up, I’m looking for an efficient system to transform an object hierarchy into a hierarchy of Dictionary<string, object> using a JSON serializer available in WinRT.

  • 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-06-15T04:07:54+00:00Added an answer on June 15, 2026 at 4:07 am

    I’m addressing this same issue in a WinRT app using a combination of the JSON.NET library and the ExpandoObject class. The library is able to deserialize JSON data quite nicely into ExpandoObjects, which implement IDictionary. The value of an ExpandoObject’s key-value pair may easily be treated as another ExpandoObject.

    Here’s the approach I used, adapted to your sample:

    void LoadJSONData()
    {
        string testData = "{ \"Name\":\"John Smith\", \"Age\":42, \"Parent\": { \"Name\":\"Brian Smith\", \"Age\":65, \"Parent\": { \"Name\":\"James Smith\", \"Age\":87, } } }";
    
        ExpandoObject dataObj = JsonConvert.DeserializeObject<ExpandoObject>(testData, new ExpandoObjectConverter());
    
        // Grab the parent object directly (if it exists) and treat as ExpandoObject
        var parentElement = dataObj.Where(el => el.Key == "Parent").FirstOrDefault();
        if (parentElement.Value != null && parentElement.Value is ExpandoObject)
        {
            ExpandoObject parentObj = (ExpandoObject)parentElement.Value;
            // do something with the parent object...
        }
    
        // Alternately, iterate through the properties of the expando
        foreach (var property in (IDictionary<String, Object>)dataObj)
        {
            if (property.Key == "Parent" && property.Value != null && property.Value is ExpandoObject)
            {
                foreach (var parentProp in (ExpandoObject)property.Value)
                {
                    // do something with the properties in the parent expando
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I would like to count the length of a string with PHP. The string
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Specifically, suppose I start with the string string =hello \'i am \' me And

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.