[英]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.