简体   繁体   English

使用 JsonConvert.DeserializeObject (Newtonsoft.Json) 反序列化时丢失不可打印的 ascii 字符(组分隔符)

[英]Losing non printable ascii character (group separator) when deserializing with JsonConvert.DeserializeObject (Newtonsoft.Json)

I have a frustrating problem that I'm unable to solve.我有一个令人沮丧的问题,我无法解决。 I am using Newtonsoft.Json to deserialize some .json data.我正在使用 Newtonsoft.Json 反序列化一些.json数据。 I can plainly see in the raw json string that there are character sequences我可以在原始 json 字符串中清楚地看到有字符序列

{ '\\', 'u', '0', '0', '1', 'd' }

This represents "group separator" (ascii 29 or 0x1D).这表示“组分隔符”(ascii 29 或 0x1D)。 However, when I deserialize using JsonConvert.DeserializeObject<> these character sequences are not put correctly into their objects.但是,当我使用JsonConvert.DeserializeObject<>反序列化时,这些字符序列没有正确放入它们的对象中。 What is put in instead is simply 1D.相反,放入的只是一维。 That is if you look at the character array you see:也就是说,如果您查看您看到的字符数组:

29 '\'

The 6 separate characters have been replaced by 1. 6 个单独的字符已替换为 1。

Does anyone know why this might happen ?有谁知道为什么会发生这种情况 I have included a test program below.我在下面包含了一个测试程序。 Unfortunately, it behaves exactly how I would expect.不幸的是,它的行为完全符合我的预期。 That is, the 6 characters show up in the objects field "Description".也就是说,6 个字符显示在对象字段“描述”中。 Clearly, I've missed something and not captured that actual problem and I realize one is supposed to try as hard as possible to come up with a small program that duplicates the problem in the large body of code.很明显,我错过了一些东西并且没有捕捉到真正的问题,我意识到应该尽可能努力地想出一个小程序,在大量代码中复制问题。 Unfortunately, I'm coming up blank.不幸的是,我来了空白。 So, I'm asking for advice on what to look for and how this could possibly happen.所以,我正在寻求关于寻找什么以及这可能如何发生的建议。 What would do this replacement on a deserialization?这个替换对反序列化有什么作用? The actual objects are more complicated than my Tanks example, but there is the ideas of IEnumerable in Tanks.实际对象比我的 Tanks 示例更复杂,但是在 Tanks 中有 IEnumerable 的想法。

class Program
{
    static void Main(string[] args)
    {
         JsonSerializerSettings settings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
        Tank tank1 = new Tank();
        char[] array1 = new char[] { '1', '2', '\\', 'u', '0', '0', '1', 'd', '3', '4' };
        tank1.Description = new string(array1);          
        tank1.Id = 1;
        Console.WriteLine("Original string (showing hidden characters");
        for (int i = 0; i < tank1.Description.ToArray().Length; i++)
        {
            Console.Write(tank1.Description.ToArray()[i] + "  ");
        }
        Console.WriteLine();

        string conversion1 = JsonConvert.SerializeObject(tank1);

        Tank deserializedTank1 = JsonConvert.DeserializeObject<Tank>(conversion1, settings);


        Console.WriteLine("Deserialized string (showing hidden characters");
        for (int i = 0; i < deserializedTank1.Description.ToArray().Length; i++)
        {
            Console.Write(deserializedTank1.Description.ToArray()[i] + "  ");
        }
        Console.WriteLine();


        Tank tank2 = new Tank() { Id = 2 };
        tank2.Description = new string(array1);
        Tank tank3 = new Tank() { Id = 3 };
        tank3.Description = new string(array1);

        Tanks tanks = new Tanks();
        tanks.Group = new [] { tank1, tank2, tank3};
        string tanksSerializedString = JsonConvert.SerializeObject(tanks,Formatting.Indented,settings);

        Tanks deserializedTanks = JsonConvert.DeserializeObject<Tanks>(tanksSerializedString, settings);

        Console.WriteLine("Deserialized Tanks");
       foreach (Tank tank in deserializedTanks.Group)
        {
            Console.WriteLine("Deserialized string (showing hidden characters");
            for (int i = 0; i < tank.Description.ToArray().Length; i++)
            {
                Console.Write(tank.Description.ToArray()[i] + "  ");
            }
            Console.WriteLine();
        }
    }
}

interface ITank
{
    int Id { get; set; }
    string Description { get; set; }
}
public class Tank : ITank
{
    public Tank() { }
    public string Description { get; set; }
    public int Id { get; set; }
}

public class Tanks
{
    public Tanks() { }
    public IEnumerable<Tank> Group { get; set; }
}

The serializer is behaving as expected.序列化程序按预期运行。 According to the JSON Standard , a sequence of characters in the pattern \\u four-hex-digits represent a single (utf16) Unicode character literal, the group separator character for \ :根据JSON 标准,模式\\u four-hex-digits的字符序列表示单个 (utf16) Unicode 字符文字,即\的组分隔符:

字符串的 JSON 标准

If you don't want that, the \\ character has to be escaped in the string: "\\\" ,如果你不想那样, \\字符必须在字符串中转义: "\\\"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Newtonsoft.Json的JsonConvert.DeserializeObject反序列化JSON <string> (jsonString) - Deserialize JSON using Newtonsoft.Json's JsonConvert.DeserializeObject<string>(jsonString) 反序列化为数据集时,JsonConvert.DeserializeObject 不起作用 - JsonConvert.DeserializeObject is not working when deserializing to a DataSet Newtonsoft.Json.JsonReaderException:在 JsonConvert.SerializeObject 之后解析 JsonConvert.DeserializeObject 时遇到意外字符 - Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing on JsonConvert.DeserializeObject after JsonConvert.SerializeObject 第一次使用 NewtonSoft (JsonConvert.DeserializeObject&lt;&gt;() 反序列化 JSON 与 System.Text.Json (JsonSerializer.Deserialize&lt;&gt;() - First time using NewtonSoft (JsonConvert.DeserializeObject<>() for deserializing JSON versus System.Text.Json (JsonSerializer.Deserialize<>() 将具有十六进制值的JSON反序列化为sbyte属性时,JsonConvert.DeserializeObject引发异常 - JsonConvert.DeserializeObject throws an exception when deserializing JSON with a hex value into an sbyte property 在JsonConvert.DeserializeObject中反序列化对象时出现意外的标记 - Unexpected token when deserializing object in JsonConvert.DeserializeObject JsonConvert.DeserializeObject 在反序列化 CouchBase 响应时抛出异常 - JsonConvert.DeserializeObject throws exception when deserializing CouchBase response 反序列化字符串时出现 JsonConvert.DeserializeObject 错误 - JsonConvert.DeserializeObject error while deserializing a string Newtonsoft.Json - DeserializeObject 在反序列化自定义类型时抛出:将值“somestring”转换为 CustomType 类型时出错 - Newtonsoft.Json - DeserializeObject throws when deserializing custom type: Error converting value "somestring" to type CustomType 转换JSON字符串C#时出现JsonConvert.DeserializeObject错误 - JsonConvert.DeserializeObject error when converting JSON string c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM