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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:52:29+00:00 2026-05-19T01:52:29+00:00

I have a JSON string and I’d like to use to compose an object.

  • 0

I have a JSON string and I’d like to use to compose an object. I don’t know about the object structure or properties yet, so I cannot code a structure using an anonymous type. I’m stuck in .NET 3.5 on this project, so I don’t have access to the new .Net 4.0 features yet. My goal is to use the converted object in a template engine similar to WebForms, Spark, or Razor to populate template items in a document with values from a model.

I have tried JavaScriptSerializer, which has a DeserializeObject method, but it outputs a key/value dictionary instead of an object that resembles the JSON object. Any other ideas?

If it helps, here’s a unit test that expresses what I’m trying to do:

    [TestClass]
    public class when_deserializing_json_to_an_object : given_a_json_serializer_context
    {
        private object _expectedSerializedObject;
        private string _jsonStringToDeserialize;
        private object _result;

        protected override void Context()
        {
            base.Context();

            _expectedSerializedObject = new
                                            {
                                                Test = "123"
                                            };

            _jsonStringToDeserialize = "{\"Test\":\"123\"}";
        }

        protected override void BecauseOf()
        {
            _result = ObjectConverter.Deserialize(_jsonStringToDeserialize);
        }

        [TestMethod]
        public void it_should_return_the_expected_object()
        {
            var modelType = _result.GetType();
            var modelProperties = modelType.GetProperties();
            var testProperty = modelProperties.SingleOrDefault(x => x.Name == "Test");

            testProperty.GetValue( _result, null ).ShouldEqual( "123" );            
        }
    }

public abstract class given_a_json_serializer_context : SpecificationBase
    {
        protected IObjectConverter ObjectConverter;
        private JavaScriptSerializer _javascriptSerializer;

        protected override void Context()
        {
            _javascriptSerializer = new JavaScriptSerializer();
            ObjectConverter = new JsonObjectConverter(_javascriptSerializer) as IObjectConverter;
        }
    }

(SpecificationBase is a class that our team uses to help us with BDD style specifications in MSTest)

So far, the production code born out of the specification above is as follows:

public class JsonObjectConverter : IObjectConverter
{
    private readonly JavaScriptSerializer _javascriptSerializer;

    public JsonObjectConverter(JavaScriptSerializer javascriptSerializer)
    {
        _javascriptSerializer = javascriptSerializer;
    }

    public object Deserialize(string json)
    {
        return _javascriptSerializer.DeserializeObject(json);
    }
}

At this point, it’s clear that the JavaScriptSerializer isn’t going to do the trick, because it just turns the JSON into a key/value pair dictionary.

  • 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-19T01:52:30+00:00Added an answer on May 19, 2026 at 1:52 am

    You can’t create an anonymous type like this but you can emit a new class.

    Anonymous types are only anonymous to you. The compiler generates an actual type for all anonymous types and at run-time there is a real type associated with the anonymous types. Those types are not generated at run-time.

    However, you can create a deserializer that uses Reflection.Emit or any similar (more powerful) library to generate a new class at run-time that has the appropriate structure and then instantiate and populate that class.

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

Sidebar

Related Questions

I have a JSON string that looks something like this: {addresses:{address:[{@array:true,@id:888888,@uri:xyz,household:{@id:44444,@uri:xyz},person:{@id:,@uri:},addressType:{@id:1,@uri:xyz,name:Primary},address1:xyz,address2:null,address3:null,city:xyz,postalCode:111111}]}} What would be
i have a json string im converting to object with a simple eval(string); heres
I have a JSON String like this $test='{var1:null,var3:null,status:{code:150,message:blah blah}}'; I want to access the
I have a json string like this: json = {'run': 'function() { console.log('running...'); }'}
I have a JSON string like this {3560 : something, 3980 : something, 4580
I have a JSON string array of objects like this. [{id:4,rank:adm,title:title 1}, {id:2,rank:mod,title:title 2},
I have one JSON that is coming in a string format. I need to
I have two JSON objects with the same structure and I want to concat
I have a JSON result that contains numerous records. I'd like to show the
Hi I have a JSON object that is a 2-dimentional array and I need

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.