Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向JavascriptSerializer传递附加参数_C#_Json - Fatal编程技术网

C# 向JavascriptSerializer传递附加参数

C# 向JavascriptSerializer传递附加参数,c#,json,C#,Json,我将JavascriptSerializer与自定义JavascriptConverter一起使用,如下所示: public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer) { SomeObjectModel TheObject = obj as SomeObjectModel; Dictionary<string, object&

我将
JavascriptSerializer
与自定义
JavascriptConverter
一起使用,如下所示:

public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
   SomeObjectModel TheObject = obj as SomeObjectModel;
   Dictionary<string, object> OutputJson = new Dictionary<string, object>();

   OutputJson.Add("SomeKey", SomeObjectModel.SomeProperty);

   return OutputJson;
}
但是,当我向函数调用添加参数时,如下所示:

OutputJson.Add(TheJsonDictionary.SomeKey, SomeObjectModel.SomeProperty);
public override IDictionary<string, object> Serialize(Dictionary<string, string> TheJsonDictionary, object obj, JavaScriptSerializer serializer)
public-override-IDictionary序列化(Dictionary-TheJsonDictionary、object-obj、JavaScriptSerializer序列化程序)
我在编译时收到一条错误消息。现在我知道了为什么会出现这个错误(抽象方法是用2个参数定义的,我传递了3个参数),我想知道如何绕过这个问题,这样我就可以传递一个字典来对键进行编码


谢谢。

据我所知,这是您所需要的:

class Program
{
    static void Main(string[] args)
    {
        var dict = new Dictionary<string, string>();
        //dict.Add("SomeKey", "SomeProperty1");
        dict.Add("SomeKey", "SomeProperty2");
        var myConverter = new MyConverter();
        var serialized = myConverter.Serialize(dict, new SomeObjectModel() { SomeProperty1 = 1, SomeProperty2 = 2 }, new MySerializer());
    }

    class MySerializer : JavaScriptSerializer
    { }

    class MyConverter : JavaScriptConverter
    {
        Dictionary<string, string> _theJsonDictionary;

        public IDictionary<string, object> Serialize(Dictionary<string, string> TheJsonDictionary, object obj, JavaScriptSerializer serializer)
        {
            _theJsonDictionary = TheJsonDictionary;
            return Serialize(obj, serializer);
        }
        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            SomeObjectModel TheObject = obj as SomeObjectModel;
            Dictionary<string, object> OutputJson = new Dictionary<string, object>();
            foreach (var item in _theJsonDictionary.Keys)
            {
                OutputJson.Add(item, TheObject.GetType().GetProperty(_theJsonDictionary[item]).GetValue(TheObject));
            }
            return OutputJson;
        }

        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            return new object();
        }

        public override IEnumerable<Type> SupportedTypes
        {
            get { return new[] { typeof(object) }.AsEnumerable(); }
        }
    }

    class SomeObjectModel
    {
        public int SomeProperty1 { get; set; }
        public int SomeProperty2 { get; set; }
    }
}
类程序
{
静态void Main(字符串[]参数)
{
var dict=新字典();
//添加(“SomeKey”、“SomeProperty1”);
添加(“SomeKey”、“SomeProperty2”);
var myConverter=新的myConverter();
var serialized=myConverter.Serialize(dict,new SomeObjectModel(){SomeProperty1=1,SomeProperty2=2},new MySerializer());
}
类MySerializer:JavaScriptSerializer
{ }
类MyConverter:JavaScriptConverter
{
字典(theJsonDictionary);;
公共IDictionary序列化(Dictionary TheJsonDictionary、object obj、JavaScriptSerializer序列化程序)
{
_theJsonDictionary=theJsonDictionary;
返回序列化(obj,序列化程序);
}
公共重写IDictionary序列化(对象obj、JavaScriptSerializer序列化程序)
{
SomeObjectModel TheObject=obj作为SomeObjectModel;
Dictionary OutputJson=新字典();
foreach(jsondictionary.key中的var项)
{
OutputJson.Add(item,TheObject.GetType().GetProperty(_theJsonDictionary[item]).GetValue(TheObject));
}
返回OutputJson;
}
公共重写对象反序列化(IDictionary dictionary、类型、JavaScriptSerializer序列化程序)
{
返回新对象();
}
公共覆盖IEnumerable SupportedTypes
{
获取{returnnew[]{typeof(object)}.AsEnumerable();}
}
}
类对象模型
{
公共int SomeProperty1{get;set;}
公共int SomeProperty2{get;set;}
}
}

据我所知,这是您所需要的:

class Program
{
    static void Main(string[] args)
    {
        var dict = new Dictionary<string, string>();
        //dict.Add("SomeKey", "SomeProperty1");
        dict.Add("SomeKey", "SomeProperty2");
        var myConverter = new MyConverter();
        var serialized = myConverter.Serialize(dict, new SomeObjectModel() { SomeProperty1 = 1, SomeProperty2 = 2 }, new MySerializer());
    }

    class MySerializer : JavaScriptSerializer
    { }

    class MyConverter : JavaScriptConverter
    {
        Dictionary<string, string> _theJsonDictionary;

        public IDictionary<string, object> Serialize(Dictionary<string, string> TheJsonDictionary, object obj, JavaScriptSerializer serializer)
        {
            _theJsonDictionary = TheJsonDictionary;
            return Serialize(obj, serializer);
        }
        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            SomeObjectModel TheObject = obj as SomeObjectModel;
            Dictionary<string, object> OutputJson = new Dictionary<string, object>();
            foreach (var item in _theJsonDictionary.Keys)
            {
                OutputJson.Add(item, TheObject.GetType().GetProperty(_theJsonDictionary[item]).GetValue(TheObject));
            }
            return OutputJson;
        }

        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            return new object();
        }

        public override IEnumerable<Type> SupportedTypes
        {
            get { return new[] { typeof(object) }.AsEnumerable(); }
        }
    }

    class SomeObjectModel
    {
        public int SomeProperty1 { get; set; }
        public int SomeProperty2 { get; set; }
    }
}
类程序
{
静态void Main(字符串[]参数)
{
var dict=新字典();
//添加(“SomeKey”、“SomeProperty1”);
添加(“SomeKey”、“SomeProperty2”);
var myConverter=新的myConverter();
var serialized=myConverter.Serialize(dict,new SomeObjectModel(){SomeProperty1=1,SomeProperty2=2},new MySerializer());
}
类MySerializer:JavaScriptSerializer
{ }
类MyConverter:JavaScriptConverter
{
字典(theJsonDictionary);;
公共IDictionary序列化(Dictionary TheJsonDictionary、object obj、JavaScriptSerializer序列化程序)
{
_theJsonDictionary=theJsonDictionary;
返回序列化(obj,序列化程序);
}
公共重写IDictionary序列化(对象obj、JavaScriptSerializer序列化程序)
{
SomeObjectModel TheObject=obj作为SomeObjectModel;
Dictionary OutputJson=新字典();
foreach(jsondictionary.key中的var项)
{
OutputJson.Add(item,TheObject.GetType().GetProperty(_theJsonDictionary[item]).GetValue(TheObject));
}
返回OutputJson;
}
公共重写对象反序列化(IDictionary dictionary、类型、JavaScriptSerializer序列化程序)
{
返回新对象();
}
公共覆盖IEnumerable SupportedTypes
{
获取{returnnew[]{typeof(object)}.AsEnumerable();}
}
}
类对象模型
{
公共int SomeProperty1{get;set;}
公共int SomeProperty2{get;set;}
}
}

我想唯一的解决办法是对
JavaScriptSerializer
进行子类化,并重新实现
Serialize
方法,以接受任意
JSONDictionary
字典:

class CustomJavaScriptSerializer : JavaScriptSerializer
{
    public Dictionary<string, string> TheJsonDictionary { get; private set; }

    public string Serialize(object obj, Dictionary<string, string> TheJsonDictionary = null)
    {
        this.TheJsonDictionary = TheJsonDictionary;
        return base.Serialize(obj);
    }
}

我想唯一的解决办法是将
JavaScriptSerializer
子类化,并重新实现
Serialize
方法以接受任意
TheJsonDictionary
字典:

class CustomJavaScriptSerializer : JavaScriptSerializer
{
    public Dictionary<string, string> TheJsonDictionary { get; private set; }

    public string Serialize(object obj, Dictionary<string, string> TheJsonDictionary = null)
    {
        this.TheJsonDictionary = TheJsonDictionary;
        return base.Serialize(obj);
    }
}

你的意思是
OutputJson.Add(“SomeKey”,TheObject.SomeProperty)
?@AlexFilipovici:这就是当前输出的内容,但是我想做的是创建一个和的字典,以便我可以编写OutputJson.Add(JSONDictionary.SomeKey,SomeObjectModel.SomeProperty);基本上,我希望键是变量,而不是硬编码到序列化器/反序列化器中?@AlexFilipovici:这就是当前输出的内容,但是我想做的是创建一个和的字典,以便我可以编写OutputJson.Add(JSONDictionary.SomeKey,SomeObjectModel.SomeProperty);基本上,我希望键是变量,而不是硬编码到序列化器/反序列化器中。不,我不希望“SomeKey”硬编码为OutputJson.Add(“SomeKey”…这不是我想要做的。我想让json键变为变量。正如您所看到的,我删除了
覆盖
方法修饰符。现在呢?我解析
\u JSONDICTIONARY
,并将其每个键/值对添加到输出中。好的,让我看一下,我会给您回复的。谢谢您的回答,应该可以eed解决了这个问题。不,我不想让“SomeKey”硬编码,所以输出json。添加(“SomeKey”…不是我想要做的。我想让json键变为变量。正如你所看到的,我删除了
覆盖
方法修饰符。现在呢?我解析
\u theJsonDictionary
并将其每个键/值对添加到