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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:31:06+00:00 2026-05-25T10:31:06+00:00

I am trying to retrieve a Dictionary of key/value pairs from my C# application

  • 0

I am trying to retrieve a Dictionary of key/value pairs from my C# application with JSON, but I am screwing up somewhere. This is my first time with JSON so I am probably just doing something stupid.

C# Code:

        else if (string.Equals(request, "getchat"))
        {
            string timestamp = DateTime.Now.ToString("yyyy.MM.dd hh:mm:ss");

            Dictionary<string, string> data = new Dictionary<string, string>();
            data.Add(timestamp, "random message");
            data.Add(timestamp, "2nd chat msg");
            data.Add(timestamp, "console line!");

            return Response.AsJson(data);
        }

Javascript:

function getChatData()
{
    $.getJSON(dataSource + "?req=getchat", "", function (data)
    {
        $.each(data, function(key, val)
        {
            addChatEntry(key, val);
        }
    });
}
  • 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-25T10:31:07+00:00Added an answer on May 25, 2026 at 10:31 am

    A dictionary is not serialized as array. Also keys in a dictionary must be unique and you will probably get an exception that a key with the same name has already be inserted when you try to run your server side code. Try using an array of values:

    var data = new[]
    {
        new { key = timestamp, value = "random message" },
        new { key = timestamp, value = "2nd chat msg" },
        new { key = timestamp, value = "console line!" },
    };
    return Response.AsJson(data);
    

    The serialized json should look something like this:

    [ 
        { "key":"2011.09.03 15:11:10", "value":"random message" }, 
        { "key":"2011.09.03 15:11:10", "value":"2nd chat msg" }, 
        { "key":"2011.09.03 15:11:10", "value":"console line!" }
    ]
    

    now in your javascript you can loop:

    $.getJSON(dataSource, { req: 'getchat' }, function (data) {
        $.each(data, function(index, item) {
            // use item.key and item.value to access the respective properties
            addChatEntry(item.key, item.value);
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to retrieve the value of a dictionary key and display that
Iam trying to retrieve data from mysql database into stylesheet.php but it is not
I'm trying to retrieve the attribute value from an xsl:param and use it in
I have a key/value pairs mapped by hash (ID) in Dictionary<string, Dictionary<string, string>> called
I am trying to use LINQ to retrieve some data from a dictionary. var
I am trying to parse a dictionary from a json file as follows {
I am trying to retrieve the value for a key in a NSDictionary. The
I am trying to retrieve generated keys from an executeBatch() transaction but I only
I am trying retrieve user name from the code through graph api, the below
I'm trying to retrieve this page using Apache HttpClient: http://quick-dish.tablespoon.com/ Unfortunately, when I try

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.