zoukankan      html  css  js  c++  java
  • web api中接收 复杂类型数组参数(对象数组参数)

      今天在工作中遇到一个问题,一个接口需要前端传递一个对象数组参数,但出现后台无法反序列对象的错误提示。

    提示信息类似:

    "Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IEnumerable`1[EZAPP.Models.ViewModel.IdAndScoreVM]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. ↵To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. ↵Path 'vMs', line 1, position 7."

      当观察传递参数的json结构时发现,

     

     还是标准的key-value形式的json,JSON 字符串。但观察后端参数,

     该需要的参数是数组 List等 集合参数。自然不会识别成key-value形式的。

      小笨的解决方案是:构造一个简单的对象,类似:

         

      因为现在参数同是一个类 一个对象,前端依然不变在Request Body中HTTP POST传递参数,如:

    前端提交参数:

        

     后端接收并解析成需要的数组/List 对象:

     

     调试一下给你看:

     

     可以看到,已经反序列化为需要的数据类型

    完~

  • 相关阅读:
    C#泛型
    QT QML Keys 处理注意事项
    Ubuntu 16.04 安装 QT Create 5.3.1
    在VMWare中安装了Ubuntu16.04,想要 Win10 中通过 SecureCRT 来操作
    Ubuntu16在VMWare中使用共享文件夹
    QT QLineEdit 获取焦点/获取焦点后全选字符
    QT Layout 布局的重要性
    QT ToolBar 工具栏浮动状态
    QT 格式化字符串功能
    QT 窗体之间(MainWindow 和 Dialog)数据传递
  • 原文地址:https://www.cnblogs.com/camefor/p/13054039.html
Copyright © 2011-2022 走看看