简体   繁体   English

从用户收到的Json无法转换为我的webserver API中的类型

[英]Json received from user cannot be converted to type in my webserver API

I developed a web API that receives json and converts it to my predefined format & then writes it in a file. 我开发了一个Web API,它接收json并将其转换为我的预定义格式,然后将其写入文件。

when client is trying to send a put request, I receive this json on server: 当客户端尝试发送put请求时,我在服务器上收到这个json:

{
  "_entities": [
    {
      "_name": "wizardaccessprivilege",
      "_SourceUniqueId": "e",
      "_isIncludedInLibrary": false,
      "_isCustomEntity": false,
      "_sourceSchemaName": "WizardAccessPrivilege",
      "_isIncludedInConnection": false,
      "_uniqueId": "9",
      "_hasChanged": "",
      "_associatedLibrary": {
        "_description": "Privilege needed to access a Web-based wizard."
      },
      "_defaultLabel": "Web Wizard Access Privilege",
      "_description": "Privilege needed to access a Web-based wizard.",
      "_sourceApplication": 0,
      "_lastModifiedDate": ""
    },
    {
      "_name": "egcsapps_fcrisksubcategory",
      "_SourceUniqueId": "7",
      "_isIncludedInLibrary": false,
      "_isCustomEntity": true,
      "_sourceSchemaName": "egory",
      "_isIncludedInConnection": false,
      "_uniqueId": "f",
      "_hasChanged": "",
      "_associatedLibrary": {
        "_description": ""
      },
      "_defaultLabel": "FC Risk Subcategory",
      "_description": "",
      "_sourceApplication": 0,
      "_lastModifiedDate": ""
    }
        ]
}

while my object structure is: 而我的对象结构是:

{
  "_sourceClientVersionStamp": "06/05/2019 15:25:27",
  "_organizationName": null,
  "_organizationServerName": null,
  "_domain": null,
  "_workspace": null,
  "_entities": [
    {
      "_uniqueId": "gg",
      "_name": "wizardaccessprivilege",
      "_sourceSchemaName": "WizardAccessPrivilege",
      "_isCustomEntity": false,
      "_isIncludedInLibrary": false,
      "_isIncludedInConnection": false,
      "_SourceUniqueId": "e",
      "_sourceApplication": 0,
      "_sourceApplicationVersion": "5.0.0.0",
      "_defaultLabel": "Web Wizard Access Privilege",
      "_localizedLabels": null,
      "_lastModifiedDate": null,
      "_fields": null,
      "_daysSinceRecordLastModified": 0,
      "_hasChanged": null,
      "_description": "Privilege needed to access a Web-based wizard.",
      "_associatedLibrary": {
        "_library": null,
        "_libraryFolder": "/",
        "_name": null,
        "_description": ""
      },
      "_entityRelationshipSet": [],
      "_connectionSet": []
    }
  ]
}

How can I convert the json received from user to my type? 如何将从用户收到的json转换为我的类型? in the request, I have a property of my object 在请求中,我有我的对象的属性

Note: I used this but didn't work: 注意:我使用了这个但是没有用:

     [Route("api/entity/UpdateConfigurationForLibrary")]
     [HttpPut]
     public void UpdateConfigurationForLibrary([FromBody] JObject data)
     {

       string output = JsonConvert.SerializeObject(data);
       EntitySet dEntitiesOnly = JsonConvert.DeserializeObject<EntitySet>(output);
..
}
Route("api/entity/UpdateConfigurationForLibrary")]
[HttpPut]
public void UpdateConfigurationForLibrary([FromBody] EntitySet entity)
{
...
}

In the case your JSON contains a list of objects, simply change the [FromBody] EntitySet entity to [FromBody] List<EntitySet> entities . 如果您的JSON包含对象列表,只需将[FromBody] EntitySet entity更改为[FromBody] List<EntitySet> entities

The framework is smart enough to map your JSON to a class type if property names are matching or if you correctly decorate your properties with [JsonProperty("propertyName")] attribute. 如果属性名称匹配或者使用[JsonProperty("propertyName")]属性正确装饰属性,则框架足够智能,可以将JSON映射到类类型。

For more information regarding [JsonProperty] attribute, you can look here . 有关[JsonProperty]属性的更多信息,请查看此处

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

相关问题 类型…的值不能转换为 - Value of type … cannot be converted to &#39;CompareEndTodayValidator&#39;无法转换为&#39;Date&#39;类型。 - 'CompareEndTodayValidator' cannot be converted to type 'Date'.? “对象”类型的对象无法转换为“数组”类型 - Object of type 'Object' cannot be converted to type 'Array' “ customObject”类型的对象无法转换为“ customObject”类型 - Object of type 'customObject' cannot be converted to type 'customObject' sqlBulkCopy:数据源中String类型的给定值无法转换为指定目标列的int类型 - sqlBulkCopy: The given value of type String from the data source cannot be converted to type int of the specified target column 数据源中String类型的给定值无法转换为指定目标列的float类型 - The given value of type String from the data source cannot be converted to type float of the specified target column 来自数据源的 String 类型的给定值无法转换为指定目标列的 int 类型。 - The given value of type String from the data source cannot be converted to type int of the specified target column.' ValueToCompare属性无法转换为“日期”类型错误 - ValueToCompare property cannot be converted to type 'Date' Error API调用的JSON返回类型 - JSON return type from API call 在Web API Controller中处理接收到的Json数据 - Manipulating the received Json Data in Web API Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM