如何让JavaScriptSerializer将子对象视为普通的旧字符串?

时间:2023-01-21 15:19:44

I have JSON coming into my program like this:

我有JSON进入我的程序,如下所示:

{
    "Foo": "some string",
    "Bar": { "Quux" : 23 }
}

How can I use JavaScriptSerializer to parse this, but treat the Bar value as a string instead of a sub-object? The code below throws an exception, as expected.

我如何使用JavaScriptSerializer来解析它,但将Bar值视为字符串而不是子对象?正如预期的那样,下面的代码抛出异常。

After deserialization, I want thing.Bar to contain the string { "Quux" : 23 }. Is there a simple way to accomplish this?

反序列化后,我想要thing.Bar包含字符串{“Quux”:23}。有没有一种简单的方法来实现这一目标?

class Thing
{
    public string Foo { get; set; }
    public string Bar { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        string json = "{ \"Foo\": \"some string\", \"Bar\": { \"Quux\": 23 }}";
        var serializer = new JavaScriptSerializer();
        var thing = serializer.Deserialize<Thing>(json);
    }
}

2 个解决方案

#1


1  

You want to implement your own JavaScriptConverter to do this... here is an example...

你想实现自己的JavaScriptConverter来做这个...这是一个例子......

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;

namespace ConsoleApplication1
{
    [Serializable]
    public class Thing
    {
        public string Foo { get; set; }
        public string Bar { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var json = "{\"Foo\":\"some string\",\"Bar\":{\"Quux\":23}}";
            var serializer = new JavaScriptSerializer();
            serializer.RegisterConverters(new JavaScriptConverter[] {
                new StringConverter()           
            });
            var thing = serializer.Deserialize<Thing>(json);

            Console.WriteLine(thing.Bar);
        }
    }

    public class StringConverter : JavaScriptConverter
    {
        public override IEnumerable<Type> SupportedTypes
        {
            get { return new ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(string) })); }
        }

        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            var i = dictionary.First();
            return "{ \"" + i.Key + "\" : " + i.Value + " }";
        }
    }
}

#2


0  

Have you tried the contents of Bar in quotes?

你在引号中试过Bar的内容吗?

{
    "Foo": "some string",
    "Bar": "{ 'Quux' : 23 }"
}

#1


1  

You want to implement your own JavaScriptConverter to do this... here is an example...

你想实现自己的JavaScriptConverter来做这个...这是一个例子......

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;

namespace ConsoleApplication1
{
    [Serializable]
    public class Thing
    {
        public string Foo { get; set; }
        public string Bar { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var json = "{\"Foo\":\"some string\",\"Bar\":{\"Quux\":23}}";
            var serializer = new JavaScriptSerializer();
            serializer.RegisterConverters(new JavaScriptConverter[] {
                new StringConverter()           
            });
            var thing = serializer.Deserialize<Thing>(json);

            Console.WriteLine(thing.Bar);
        }
    }

    public class StringConverter : JavaScriptConverter
    {
        public override IEnumerable<Type> SupportedTypes
        {
            get { return new ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(string) })); }
        }

        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            var i = dictionary.First();
            return "{ \"" + i.Key + "\" : " + i.Value + " }";
        }
    }
}

#2


0  

Have you tried the contents of Bar in quotes?

你在引号中试过Bar的内容吗?

{
    "Foo": "some string",
    "Bar": "{ 'Quux' : 23 }"
}