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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:28:02+00:00 2026-06-14T03:28:02+00:00

Parsing json returned from the controller. For some reason when returning a dictionary I

  • 0

Parsing json returned from the controller. For some reason when returning a dictionary I need to do “ToString()” on the key element, otherwise I get an error. Why?. Are the samples below the correct way/best way to serialize json? thanks

Controller:

    // JSON
    public ActionResult GizmosJsonObject()
    {
        var gizmo = new Gizmo
        {
            Id = 2343,
            Name = "Some awesome name",
            Quantity = 32,
            IntroducedDate = DateTime.Now
        };

        return this.Json(gizmo);
    }

    public ActionResult GizmosJsonObjectCollection()
    {
        var gizmos = new List<Gizmo>();
        gizmos.Add(new Gizmo
        {
            Id = 102,
            Name = "some name1",
            Quantity = 535,
            IntroducedDate = DateTime.Now
        });

        gizmos.Add(new Gizmo
        {
            Id = 122,
            Name = "some name1",
            Quantity = 135,
            IntroducedDate = DateTime.Now
        });

        gizmos.Add(new Gizmo
        {
            Id = 562,
            Name = "some name1",
            Quantity = 2,
            IntroducedDate = DateTime.Now
        });

        return this.Json(gizmos);
    }

    public ActionResult GizmosJsonListInts()
    {
        var gizmos = new List<int>();
        gizmos.Add(2);
        gizmos.Add(56);
        gizmos.Add(32);

        return this.Json(gizmos);
    }

    public ActionResult GizmosJsonDictionaryInts()
    {
        var gizmos = new Dictionary<int, int>();
        gizmos.Add(23, 123);
        gizmos.Add(26, 227);
        gizmos.Add(54, 94323);

        return this.Json(gizmos.ToDictionary(x => x.Key.ToString(), y => y.Value));
    }

    public ActionResult GizmosJsonDictionaryStrings()
    {
        var gizmos = new Dictionary<string, string>();
        gizmos.Add("key1", "value1");
        gizmos.Add("Key2", "value2");
        gizmos.Add("key3", "value3");

        return this.Json(gizmos);
    }

View:

<script type="text/javascript">
/*<![CDATA[*/
    $(function () {

        // json object
        $("a.Object").click(function (e) {
            e.preventDefault();
            $.ajax({
                url: '@Url.Action("GizmosJsonObject", "Home")',
                contentType: 'application/json',
                type: 'POST',
                success: function (json) {
                    console.log(json.Id);
                    console.log(json.Name);
                    console.log(json.IntroducedDate);

                    // format date
                    var date = new Date(parseInt(json.IntroducedDate.substr(6)));
                    console.log(date);
                }
            });
        });

        // json object collection
        $("a.ObjectCollection").click(function (e) {
            e.preventDefault();
            $.ajax({
                url: '@Url.Action("GizmosJsonObjectCollection", "Home")',
                contentType: 'application/json',
                type: 'POST',
                success: function (json) {
                    $(json).each(function () {
                        console.log(this.Id);
                        console.log(this.Name);
                        console.log(this.IntroducedDate);

                        // format date
                        var date = new Date(parseInt(this.IntroducedDate.substr(6)));
                        console.log(date);
                    });
                }
            });
        });

        // json list of ints
        $("a.ListInts").click(function (e) {
            e.preventDefault();
            $.ajax({
                url: '@Url.Action("GizmosJsonListInts", "Home")',
                contentType: 'application/json',
                type: 'POST',
                success: function (json) {
                    $(json).each(function (i, e) {
                        console.log(json[i]);
                    });
                }
            });
        });

        // json dictionary of ints
        $("a.DictionaryInts").click(function (e) {
            e.preventDefault();
            $.ajax({
                url: '@Url.Action("GizmosJsonDictionaryInts", "Home")',
                contentType: 'application/json',
                type: 'POST',
                success: function (json) {
                    for (var key in json) {
                        if (json.hasOwnProperty(key)) {
                            var value = json[key];
                            console.log(key);
                            console.log(value);
                        }
                    }
                }
            });
        });

        // json dictionary of strings
        $("a.DictionaryStrings").click(function (e) {
            e.preventDefault();
            $.ajax({
                url: '@Url.Action("GizmosJsonDictionaryStrings", "Home")',
                contentType: 'application/json',
                type: 'POST',
                success: function (json) {
                    for (var key in json) {
                        if (json.hasOwnProperty(key)) {
                            var value = json[key];
                            console.log(key);
                            console.log(value);
                        }
                    }
                }
            });
        });
    });
/*]]>*/
</script>
  • 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-14T03:28:03+00:00Added an answer on June 14, 2026 at 3:28 am

    A JSON key must be of the type string. See the right sidebar here http://www.json.org/ where it states that a pair must take the form of string: value.


    Just for corroboration, this document here http://www.ietf.org/rfc/rfc4627.txt states the following:

    2.2. Objects

    An object structure is represented as a pair of curly brackets
    surrounding zero or more name/value pairs (or members). A name is a
    string. A single colon comes after each name, separating the name
    from the value. A single comma separates a value from a following
    name. The names within an object SHOULD be unique.

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

Sidebar

Related Questions

I need help with parsing of JSON feed text returned from Twitter. I need
I'm having difficulty parsing some JSON data returned from my server using jQuery.ajax() To
I have some Json being returned from facebook which I'm then parsing in to
I'm parsing a JSON array returned from YouTube, and while doing so I created
I am parsing the string returned from Cakephp 2.0 Controller.. My Autocomplete code is..
I'm having a bit of trouble parsing some returned JSON. I'm fairly new to
I m using JSON and while parsing, i m getting the returned value as
I'm parsing JSON that is contained in an HTML element. Consider this markup: <div
From a previous question on Stackoverflow Iterating through/Parsing JSON Object via JavaScript .... My
I have a list of places in listview which i got from parsing JSON.

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.