【问题标题】:There is no argument that corresponds to the required formal parameter没有对应于所需形式参数的参数
【发布时间】:2017-09-30 10:52:28
【问题描述】:

这可能不是最好的方法,但它是我知道如何用 c# 做的最好的方法。我正在尝试创建一个字典,然后稍后将其转换为 json。现在我只是想让字典匹配我以后想要的 json 格式。这是我到目前为止所拥有的:

`Dictionary<Dictionary<string, string>, Dictionary<string, List<List<Decimal>>>> testDict = new Dictionary<Dictionary<string, string>, Dictionary<string, List<List<Decimal>>>>() {
    new Dictionary<string, string>() {
        { "test", "test" }
    }
};`

这给了我以下错误:

没有与所需形参对应的参数

我不知道是什么原因造成的,任何帮助都会很棒,谢谢!

这是我要复制的 json 结构:

[
  {
    "target": "1",
    "datapoints": [
      [
        67.0,
        1491609600.0
      ]
    ]
  },
  {
    "target": "2",
    "datapoints": [
      [
        54.0,
        1491091200.0
      ],
      [
        65.0,
        1491177600.0
      ],
      [
        69.0,
        1491609600.0
      ],
      [
        65.0,
        1491696000.0
      ],
      [
        54.0,
        1491868800.0
      ],
      [
        63.0,
        1491955200.0
      ],
      [
        64.0,
        1492214400.0
      ],
      [
        57.0,
        1492732800.0
      ],
      [
        72.0,
        1492819200.0
      ],
      [
        50.0,
        1493337600.0
      ],
      [
        63.0,
        1493424000.0
      ]
    ]
  },
]

【问题讨论】:

  • 看起来您缺少初始化程序中的值。您已经指定了一个键 (new Dictionary&lt;string, string&gt;() { { "test", "test" }},但您缺少初始化程序的 , value 部分。要解决编译问题,请尝试以下操作:Dictionary&lt;Dictionary&lt;string, string&gt;, Dictionary&lt;string, List&lt;List&lt;Decimal&gt;&gt;&gt;&gt; testDict = new Dictionary&lt;Dictionary&lt;string, string&gt;, Dictionary&lt;string, List&lt;List&lt;Decimal&gt;&gt;&gt;&gt;() { { new Dictionary&lt;string, string&gt; { { "test", "test" } }, new Dictionary&lt;string, List&lt;List&lt;decimal&gt;&gt;&gt;() } };
  • 100% 确定结构完全错误...如果您发布所需的 JSON 结构,也许有人可以发布您需要的真实类。
  • @Gusman 看看我更新的问题 :)
  • 看起来应该是List&lt;SomeType&gt;,其中SomeType 是具有TargetDataPoints 属性的自定义类......你真的不想要你的类型此刻宣布。
  • Dictionary&lt;string, string&gt; 不能作为字典的键,因为它没有有意义的哈希码/等于...

标签: c# asp.net json


【解决方案1】:

好的,这不是您问题的答案,但它会有所帮助,是您的 JSON 的正确结构:

public class TargetClass
{
    public string target{ get; set; }
    public List<double[]> datapoints{ get; set; }
}

这是基类。如果您想反序列化 JSON 中的内容,您将执行以下操作(假设您使用的是 Newtonsoft Json,否则更改为您使用的库):

var data = Newtonsoft.Json.JsonConvert.DeserializeObject<TargetClass[]>(theString);

要序列化,您将创建如下内容:

var items = new List<TargetClass>();

var target = new TargetClass{ target = "1", datapoints = new List<double[]>{ new double[]{ 67.0, 1491609600.0 } };

items.Add(target);

var ser = Newtonsoft.Json.JsonConvert.SerializeObject(items);

【讨论】:

    【解决方案2】:

    使用匿名类型,您可以创建复杂的对象层次结构,几乎就像编写纯 JSON 一样简单:

    var obj = new[] {
        new {
            target = "1",
            datapoints = new [] {
                new [] {
                    67.0,
                    1491609600.0
                }
            }
        },
        new {
            target = "2",
            datapoints = new [] {
                new [] {
                    54.0,
                    1491091200.0
                },
                new [] {
                    65.0,
                    1491177600.0
                },
            }
        }
    };
    
    var json = JsonConvert.SerializeObject(obj, Formatting.Indented);
    

    演示:https://dotnetfiddle.net/jk8gho


    如果此对象中的子集合需要扩展,则最好将这些集合明确定义为List&lt;dynamic&gt;。严格来说,dynamic 不是必需的,可以使用显式类型代替,但使用dynamic 可以简化定义。

    var obj = new List<dynamic> {
        new {
            target = "1",
            datapoints = new List<dynamic> {
                new [] {
                    67.0,
                    1491609600.0
                }
            }
        },
        new {
            target = "2",
            datapoints = new List<dynamic> {
                new [] {
                    54.0,
                    1491091200.0
                },
                new [] {
                    65.0,
                    1491177600.0
                },
            }
        }
    };
    
    var target2 = obj.Where(t => t.target == "2").Single();
    target2.datapoints.Add(new [] {
        64.0,
        1492214400.0
    });
    target2.datapoints.Add(new[] {
        57.0,
        1492732800.0
    });
    
    var target3 = new {
        target = "3",
        datapoints = new List<dynamic> { }
    };
    target3.datapoints.Add(new[] {
        72.0,
        1492819200.0
    });
    obj.Add(target3);
    
    var json = JsonConvert.SerializeObject(obj, Formatting.Indented);
    

    演示:https://dotnetfiddle.net/d4ZUH8

    【讨论】:

    • 如何解决问题?
    • @CamiloTerevinto:OP 表达的主要问题是处理复杂的对象定义,它应该映射到某种结构的 JSON。使用建议的方法,这种映射很简单。
    • @DmitryEgorov 我同意,这是非常直接的,我喜欢这样。有没有办法在循环数据时向其中添加更多目标对象?
    • 您发布的结构不允许您反序列化对象,甚至不能从其他方法引用它。
    • @JoeScotto:是的,这是可能的,但您必须将 new [] 更改为 new List&lt;dynamic&gt; 才能扩展您想要扩展的集合。请查看更新后的答案。
    猜你喜欢
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 2019-06-30
    • 1970-01-01
    相关资源
    最近更新 更多