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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:30:13+00:00 2026-06-02T08:30:13+00:00

I am trying to serialize a .net object contains another data contract object as

  • 0

I am trying to serialize a .net object contains another data contract object as a parameter. The code is as follows…

[DataContract]
        public class JsonObject2
        {
            [DataMember(Name = "field1")]
            string field1 { get; set; }
            [DataMember(Name = "field2")]
            string field2 { get; set; }
            [DataMember(Name = "field3")]
            object[][] test = { new object[]{"key1", "value1"}, new object[]{"key2", "value2"}, new object[]{"key3", "value3"} };
        }

        [DataContract]
        public class JsonObject3
        {
            [DataMember(Name = "field1")]
            public string field1 { get; set; }
            [DataMember(Name = "field2")]
            public object field2 { get; set; }
        }

DataContractJsonSerializer dcjs2 = new DataContractJsonSerializer(typeof(JsonObject3));

                JsonObject3 obj3 = new JsonObject3();
                obj3.field1 = "hello";
                obj3.field2 = new JsonObject2();
                dcjs2.WriteObject(s, obj3);
                s.Position = 0;
                MessageBox.Show(new StreamReader(s).ReadToEnd());

Doing the above, results in the following exception…

“{“Type ‘JSONParser.Form1+JsonObject2’ with data contract name ‘Form1.JsonObject2:http://schemas.datacontract.org/2004/07/JSONParser’ is not expected. Add any types not known statically to the list of known types – for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.”}”

I can’t figure out how to carry out any of the recommendations given in that exception.

  1. How do you add to the list of KnownTypes ?
  2. How do you use the KnownTypeAttribute ?
  • 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-02T08:30:14+00:00Added an answer on June 2, 2026 at 8:30 am

    Ok, figured out how to do it. I didn’t want to delete this question in case someone else finds it useful, so here is the solution…

    [DataContract]
        [KnownType(typeof(JsonObject2))]
        public class JsonObject2
        {
            [DataMember(Name = "field1")]
            string field1 { get; set; }
            [DataMember(Name = "field2")]
            string field2 { get; set; }
            [DataMember(Name = "field3")]
            object[][] test = { new object[]{"key1", "value1"}, new object[]{"key2", "value2"}, new object[]{"key3", "value3"} };
        }
    
        [DataContract]
        public class JsonObject3
        {
            [DataMember(Name = "field1")]
            public string field1 { get; set; }    
            [DataMember(Name = "field2")]
            public object field2 { get; set; }
        }
    

    Notice the [KnownType(typeof(JsonObject2))] above the declaration of the JsonObject2 class ? That solved it.

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

Sidebar

Related Questions

I am trying to serialize a C# object to JSON using JSON.net library. The
I'm trying to properly write code to build a data structure to serialize into
I am trying to serialize a .NET TimeSpan object to XML and it is
I have a very simple class that I'm trying to serialize: [ProtoContract] public class
In Scala 2.8 and liftweb.net 2.0 I'm trying to serialize a case-class to Json
I'm trying to serialize some objects obtained from a 3rd Party .NET Lib to
Ran into an Out of Stack Space error trying to serialize an ASP.Net AJAX
I'm trying to serialize a Type object in the following way: Type myType =
I am trying to serialize an object to a sql compact database. I am
I'm trying to serialize an object to XML that has a number of properties,

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.