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

  • Home
  • SEARCH
  • 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 7839473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:27:17+00:00 2026-06-02T15:27:17+00:00

I need to create the following JavaScript object literal in C# code, as a

  • 0

I need to create the following JavaScript object literal in C# code, as a string, and am looking for some tips on how to best do this.

model: {
    id: "Id",
    fields: {
        Surname: { type: "string", validation: { required: true } },
        FirstName: { type: "string", validation: { required: true } },
        PrivateEmail: { type: "string", validation: { required: true } },
        DefaultPhone: { type: "string" },
        CompanyName: { type: "string" },
        CreateDate: { type: "date" },
        LastLoginDate: { type: "date" },
        IsLockedOut: { type: "boolean" }
    }
}

This defines a client side object with a model property that reflects what each row in my MVC4 view model will look like. I can use plain reflection to generate a string literal, but I would rather somehow tap into already available JSON serialization services in .NET. To do so I think I would need to create an anonymous object with properties corresponding to the JS properties above. How could I do this?

EDIT:
I need to loop over the properties in a view model class and generated a C# object that will serialize to a JavaScript ‘transform’ of the view model class similar to the one above.

  • 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-02T15:27:18+00:00Added an answer on June 2, 2026 at 3:27 pm

    What I think he may be asking is to convert a C# object to a JSON string.

    Try this:

    http://msdn.microsoft.com/en-us/library/system.json.jsonobject%28v=vs.95%29.aspx

    or

    http://james.newtonking.com/pages/json-net.aspx

    EDIT (an example on how to use):

    Product product = new Product();
    product.Name = "Apple";
    product.Expiry = new DateTime(2008, 12, 28);
    product.Price = 3.99M;
    product.Sizes = new string[] { "Small", "Medium", "Large" };
    
    string json = JsonConvert.SerializeObject(product);
    //{
    //  "Name": "Apple",
    //  "Expiry": new Date(1230422400000),
    //  "Price": 3.99,
    //  "Sizes": [
    //    "Small",
    //    "Medium",
    //    "Large"
    //  ]
    //}
    
    Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
    

    EDIT:

    This is the comment from @yyamil:

    You can also use anonymous objects in case you don’t want to create a new class just to serialize a json object:

    var notificationPayload = new
    {
        notification = new
        {
            title = "Title", 
            body = "body"
        }
    };
    
    string json = JsonConvert.SerializeObject(notificationPayload);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code. I need to create list separators before A elements,
I have a situation, where I need to create a new JavaScript object that
Recently I saw the following code that creates a class in javascript: var Model.Foo
I essentially need to create a version of the following website using JavaScript and
I need to create following functionality in javascript: User should be able to get
I have the following code that creates an object in JavaScript. It uses prototype
I need to create the following layout: +--------+----------------------------------+-----------+ | fixed | variable | variable
I have the following three arrays and need to create a new two-dimensional array
I need to develop an android app that achieves the following: Create an activity
Hi I have the following interfaces that I need to create. Can you just

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.