我开发了一个web API,它接收json并将其转换为我预定义的格式,然后将其写入文件中。
当客户端尝试发送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": ""
}
]
}而我的对象结构是:
{
"_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": []
}
]
}如何将从user收到的json转换为我的类型?在请求中,我有我的对象的一个属性
注意:我使用了这个,但不起作用:
[Route("api/entity/UpdateConfigurationForLibrary")]
[HttpPut]
public void UpdateConfigurationForLibrary([FromBody] JObject data)
{
string output = JsonConvert.SerializeObject(data);
EntitySet dEntitiesOnly = JsonConvert.DeserializeObject<EntitySet>(output);
..
}发布于 2019-06-13 04:05:50
Route("api/entity/UpdateConfigurationForLibrary")]
[HttpPut]
public void UpdateConfigurationForLibrary([FromBody] EntitySet entity)
{
...
}如果您的JSON包含一个对象列表,只需将[FromBody] EntitySet entity更改为[FromBody] List<EntitySet> entities。
如果属性名称匹配,或者如果您使用[JsonProperty("propertyName")]属性正确地修饰了属性,那么框架就足够智能,可以将您的JSON映射到类类型。
有关[JsonProperty]属性的更多信息,可以查看here。
https://stackoverflow.com/questions/56569615
复制相似问题