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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:33:01+00:00 2026-05-29T17:33:01+00:00

Background I am working on an application that requires the consumption of JSON services

  • 0

Background

I am working on an application that requires the consumption of JSON services on the server, and then repackaging of that JSON into a view model that can be referenced using Razor syntax in the view. Server-side manipulation of this code is a must for various reasons.

We are using C#, .NET 4, MVC3, Razor, JsonFx.

We currently have code that works just fine, but it is taking up to a minute to iterate 250 items in the received JSON object and this is unacceptable (not to mention baffling). I have already isolated the problem to the following loop; the JSON comes in lightening-fast so that is not the problem. Here is the working but extremely slow code:

        var reader = new JsonReader();
        var json = GetJson(SPListName);

        var admItems = new List<IDictionary<String, object>>();
        dynamic _items = reader.Read(json); //This part is REALLY fast.  No problem here.
        foreach (var itm in _items)
        {
            dynamic obj = new ExpandoObject();
            foreach (dynamic admObj in itm)//Here begins the slow part.
            {
                var item = obj as IDictionary<String, object>;
                var encodedValue = "";
                try
                {
                    if(admObj.Key == "Title")
                    {
                        encodedValue = admObj.Value.ToString();
                    }else
                    {
                        encodedValue = admObj.Value[0].ToString();
                    }
                }
                catch (Exception)
                {
                    encodedValue = admObj.Value.ToString();                   
                }

                item[admObj.Key] = encodedValue.EncodeNonAscii().FixHtmlEntities();
            }
            admItems.Add(obj);
        }
        return admItems;

You may also notice a few custom extension methods. Here they are (in case that matters):

public static string EncodeNonAscii(this Object str)
            {
                StringBuilder sb = new StringBuilder();
                foreach (char c in str.ToString())
                {
                    if (c > 127)
                    {
                        // This character is too big for ASCII
                        string encodedValue = "\\u" + ((int) c).ToString("x4");
                        sb.Append(encodedValue);
                    }
                    else
                    {
                        sb.Append(c);
                    }
                }
                return sb.ToString();
            }

            public static string FixHtmlEntities(this Object str)
            {
                var fixedString = str.ToString().Replace("\\u00c2\\u00ae", "&reg;");
                return fixedString;
            }

Question

What the heck am I doing wrong/how do I speed this up. My brain is hamburger right now so I hope someone points out a simple oversight.

Update/Resolution

Rophuine and Joshua Enfield both pointed to the root of the speed issue: the catching of exceptions was slowing everything down.

Many folks suggested that I use Json.Net or something similar. While I appreciate that advice, it really wasn’t at the root of my problem (even though it may have appeared that way); I have used Json.Net extensively in the past and came to prefer JsonFx over it a few months ago. In this particular case, I am more concerned with the construction of a view model object as I have already deserialized the JSON using JsonFx. Please let me know if you still think I am missing your point ;).

The reason for my brain-dead try/catch scheme was that I needed to do different things with each property of the JSON string depending on it’s type (string, string[], object[], etc). I forgot that System.Type can handle that for me. So here is the final code:

var reader = new JsonReader();
                var json = GetJson(SPListName);

                var admItems = new List<IDictionary<String, object>>();
                dynamic _items = reader.Read(json);
                foreach (var itm in _items)
                {
                    dynamic obj = new ExpandoObject();
                    foreach (dynamic admObj in itm)
                    {
                        var item = obj as IDictionary<String, object>;
                        var encodedValue = "";

                        Type typeName = admObj.Value.GetType();

                        switch (typeName.ToString())
                        {
                            case("System.String[]"):
                                encodedValue = admObj.Value[0].ToString();
                                break;
                            default:
                                encodedValue = admObj.Value.ToString();
                                break;
                        }

                        item[admObj.Key] = encodedValue.EncodeNonAscii().FixHtmlEntities();
                    }
                    admItems.Add(obj);
                }
                return admItems;

Hope this helps someone!

  • 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-29T17:33:03+00:00Added an answer on May 29, 2026 at 5:33 pm
                try
                {
                    if(admObj.Key == "Title")
                    {
                        encodedValue = admObj.Value.ToString();
                    }else
                    {
                        encodedValue = admObj.Value[0].ToString();
                    }
                }
                catch (Exception)
                {
                    encodedValue = admObj.Value.ToString();                   
                }
    

    I don’t have a C# compiler in front of me, or access to your data, but this looks suspect to me. Exceptions are extremely slow – are you hitting the catch block often? If you are, try to work out what’s causing the exceptions and handle them without causing an exception – leave exceptions to handle rare situations you haven’t thought of.

    Edit: Final solution is in the question edit – I won’t repeat it here. If you have a performance problem and you have exception-handling somewhere in your loop, that’s often the very first thing to try and eliminate. Exceptions are awfully slow – much more so than you might think. They’re best kept for very unusual circumstances.

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

Sidebar

Related Questions

Background Currently, I am working on a Rails application. I have different products that
Background I'm working on an application which requires user-entered content, and I've decided to
Background I, like scores of programmers before me, am working on an application that
Background I'm working on building an ASP.Net MVC 3 application that utilizes: JQueryUI for
A little background I'm working on an .net application that's uses plugins heavily, the
I'm a person with a non-programming background working on a web application that must
Problem Background I am currently working on a camel based ETL application that processes
A bit of background: I am currently working on an application that allows novice
Background: I am working on a web application, that I plan to launch with
First some background: I'm working on an application and I'm trying to follow MVVM

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.