SoFunction
Updated on 2025-04-11

Solve the problem of garbled JSON based on .NET

During the development process, we often encounter problems in JSON data processing, especially in the process of data transmission and parsing, which are prone to garbled problems caused by encoding errors. This usually happens when the original data is encoded in UTF-8 but is decoded incorrectly with other encodings such as GBK. To solve this problem, I developed a .NET tool class called JsonEncodingFixer, which can effectively fix JSON garbled problems caused by encoding errors.

Problem background

In actual development, encoding and decoding of JSON data is a common link. However, when data is transmitted between different systems, garbled code may occur due to inconsistent encoding. For example:

  • The raw data is stored in UTF-8 encoding.
  • During transmission or parsing, the data is incorrectly encoded and decoded with GBK.
  • Eventually, it leads to garbled code in the JSON string.

This problem not only affects the readability of the data, but may also lead to failure of subsequent processing. Therefore, we need a tool to fix this encoding error.

Core Principle

The core principle of JsonEncodingFixer is to fix garbled code through the following steps:

Reverse engineering: Re-encodes the wrongly decoded string into the original error byte.

Correct decoding: Reparse these bytes using the correct encoding (such as UTF-8).

Specifically, we first convert the garbled string into a byte array with GBK encoding, and then re-parse the bytes with UTF-8 encoding to restore the original correct string.

Tool class implementation

The following is the complete code implementation of the JsonEncodingFixer tool class, including detailed comments and instructions:

using System;
using ;
using ;
using ;
using ;

namespace HalconCenter
{
    /// <summary>
    /// JSON encoding repair tool class    /// 🌟 Applicable scenarios: Fix the JSON garbled problem caused by encoding errors. Typical scenarios are:    /// 1. The raw data is encoded using UTF-8    /// 2. Incorrectly decoded with GBK and other non-UTF-8 encoding    /// 3. Causes garbled JSON strings    /// </summary>
    public class JsonEncodingFixer
    {
        /// <summary>
        /// Fix a single wrongly encoded string        /// 🌟 Core principle: Error decoding -> Restore original error bytes -> Correct encoding and redecoding        /// </summary>
        /// <param name="garbledText">Garbled string (result of UTF-8 bytes being misused with GBK decoding)</param>        /// <returns>Fixed correct string</returns>        public static string FixEncoding(string garbledText)
        {
            try
            {
                // 🌟 Register extended encoding support (.NET Core does not include GBK and other encodings by default)                ();

                // 🌟 Reverse engineering: Re-encoded the wrongly decoded string into the original error byte                // Equivalent to: the inverse process of error decoding, obtaining the error bytes during the original transmission                byte[] wrongBytes = ("GBK").GetBytes(garbledText);

                // 🌟 Correct decoding: Re-parse the original bytes with the supposedly UTF-8 encoding                return Encoding.(wrongBytes);
            }
            catch (Exception ex)
            {
                // 🌟Exception handling principle: Ensure business continuity, rather than return garbled code than block the process                ($"Encoding failed: {}");
                return garbledText;
            }
        }

        /// &lt;summary&gt;
        /// Automatically repair the entire JSON object        /// 🌟 Implementation strategy:        /// 1. Parsing the original JSON structure        /// 2. Deep traversal of all nodes        /// 3. Fix the encoding of each string node        /// 4. Rebuild the JSON structure and maintain the format        /// &lt;/summary&gt;
        /// <param name="json">JSON string that needs to be fixed</param>        /// <returns>Fix encoded JSON string</returns>        public static string FixJsonEncoding(string json)
        {
            // 🌈 Use JsonDocument to parse instead of deserialization to avoid type conversion interference            using (JsonDocument doc = (json))
            using (var ms = new MemoryStream())
            {
                // 🌟 Key configuration: Set loose JSON encoding rules (prevent quadratic escaping)                var options = new JsonWriterOptions
                {
                    Indented = true,                          // Maintain beautiful format                    Encoder =  // Special characters such as Chinese are allowed                };

                // 🚀 Rebuild JSON using Utf8JsonWriter                using (var writer = new Utf8JsonWriter(ms, options))
                {
                    WriteFixedValue(, writer);
                }
                return Encoding.(());
            }
        }

        /// &lt;summary&gt;
        /// Recursively write the fixed JSON value        /// 🌟 Traversal strategy:        /// 1. Object: Fix each attribute name and attribute value        /// 2. Array: Fix each element        /// 3. String: Apply FixEncoding to fix        /// 4. Other types: Keep the original value        /// &lt;/summary&gt;
        /// <param name="element">Current JSON element</param>        /// <param name="writer">JSON Writer</param>        private static void WriteFixedValue(JsonElement element, Utf8JsonWriter writer)
        {
            switch ()
            {
                case :
                    ();
                    foreach ( prop in ())
                    {
                        // 🔄 Double fix: attribute name and attribute value need to be processed                        var fixedName = FixEncoding();
                        (fixedName);
                        WriteFixedValue(, writer);
                    }
                    ();
                    break;

                case :
                    ();
                    foreach (JsonElement item in ())
                    {
                        // 🔄 Recursively process array elements                        WriteFixedValue(item, writer);
                    }
                    ();
                    break;

                case :
                    // 🎯 Core repair point: string value repair                    (FixEncoding(()));
                    break;

                default:
                    // ⚙️ Non-string type is written directly (number/boolean/null, etc.)                    (writer);
                    break;
            }
        }
    }
}

Code description

method:

This is the core fix for fixing a single garbled string.

It restores the original string by re-encoding the wrongly decoded string into a byte array and then re-parsing it with the correct encoding.

method:

This method is used to repair the entire JSON object.

It parses JSON using JsonDocument, then iterates through all nodes deeply, fixing each string value.

Finally, it rebuilds the JSON structure through Utf8JsonWriter, leaving the format unchanged.

method:

This is a recursive method for deep traversing JSON objects or arrays.

It fixes every string value and correctly handles other types of nodes (such as numbers, booleans, null, etc.).

Example of usage

Here is a simple example of usage:

using System;

namespace ExampleUsage
{
    class Program
    {
        static void Main(string[] args)
        {
            string garbledText = "日本语"; // Example garbled text            string yourCorruptedJson = "{\"name\":\"日本语\",\"age\":30}";

            // Fix single string            string fixedString = (garbledText);
            ($"Fixed string: {fixedString}");

            // Fix the entire JSON object            string fixedJson = (yourCorruptedJson);
            ($"RepairedJSON: {fixedJson}");
        }
    }
}

The output result will be the correct JSON string after the fix.

Summarize

JsonEncodingFixerIt is a simple and powerful tool class that can help us quickly fix JSON garbled problems. It is suitable for various garbled scenarios caused by encoding errors, and can significantly improve development efficiency. If you encounter similar garbled problems in your project, you might as well try this tool class.

This is the article about solving the problem of garbled JSON based on .NET writing tools. For more related garbled JSON content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!