首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将从用户接收的Json转换为我的API服务器API中的类型

无法将从用户接收的Json转换为我的API服务器API中的类型
EN

Stack Overflow用户
提问于 2019-06-13 04:03:21
回答 1查看 13关注 0票数 0

我开发了一个web API,它接收json并将其转换为我预定义的格式,然后将其写入文件中。

当客户端尝试发送put请求时,我在服务器上收到以下json:

代码语言:javascript
复制
{
  "_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": ""
    }
        ]
}

而我的对象结构是:

代码语言:javascript
复制
{
  "_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转换为我的类型?在请求中,我有我的对象的一个属性

注意:我使用了这个,但不起作用:

代码语言:javascript
复制
     [Route("api/entity/UpdateConfigurationForLibrary")]
     [HttpPut]
     public void UpdateConfigurationForLibrary([FromBody] JObject data)
     {

       string output = JsonConvert.SerializeObject(data);
       EntitySet dEntitiesOnly = JsonConvert.DeserializeObject<EntitySet>(output);
..
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-13 04:05:50

代码语言:javascript
复制
Route("api/entity/UpdateConfigurationForLibrary")]
[HttpPut]
public void UpdateConfigurationForLibrary([FromBody] EntitySet entity)
{
...
}

如果您的JSON包含一个对象列表,只需将[FromBody] EntitySet entity更改为[FromBody] List<EntitySet> entities

如果属性名称匹配,或者如果您使用[JsonProperty("propertyName")]属性正确地修饰了属性,那么框架就足够智能,可以将您的JSON映射到类类型。

有关[JsonProperty]属性的更多信息,可以查看here

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56569615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档