如何从数据库SQL读取Json并将其转换为Java类?

问题描述

我在数据库的表中使用JSON Auto创建了一个varchar(max)类型的列,以便该表的所有列都以JSON格式存储,包括外键。

现在我创建的我的Json看起来像这样:

{
    "widgetConfiguration": {
        "id": 1,"name": "sfgg","is_cache_enabled": null,"xparam": null,"xparamvalue": null,"yparam": null,"yparamvalue": null,"operation": null,"fromdate": "2020-04-13T00:00:00","todate": null,"categorizeon": null,"remarks": "t1","limit1": null,"filter": null,"description": "","add_to_summary": "#EA1136","summaryname": "#E19452","colour": "card t-4","subtitle_alias": null,"x_axis_alias": null,"y_axis_alias": null,"summary_subtitle_alias": null,"is_deleted": false,"sub_x_axis": null,"sub_x_axis_alias": null,"category": "domain-category","imgsrc": ".\/assets\/image\/Cyber_governence.png","count_category": null,"count_column": null,"count_category_column": null,"drilldown": null,"y2axis": null,"dualaxis": null,"threshold_field": null,"createdby": null,"createdon": null,"modifiedby": "afgafg","modifieddate": "2020-04-13T00:00:00","showOnCategories": null,"priority": 1,"link": null,"percent_stacked": null,"y1axistype": null,"y2axistype": null,"note": null,"drilldown_title1": null,"drilldown_title2": null,"drilldown_type": "Normal","severity": null,"context": null,"records_type": null,"charttype":"{\"id\":1,\"charttype\":\"line\",\"description\":\"Line chart\",\"context\":2,\"default_config\":\"{\\\"chart\\\":{\\\"type\\\":\\\"line\\\",\\\"zoomType\\\":\\\"xy\\\"},\\\"title\\\":null,\\\"legend\\\":{\\\"layout\\\":\\\"vertical\\\",\\\"align\\\":\\\"right\\\",\\\"verticalAlign\\\":\\\"middle\\\"},\\\"plotOptions\\\":{\\\"series\\\":{\\\"dataLabels\\\":{\\\"enabled\\\":true}}}}\",\"createdby\":null,\"createdon\":null,\"updatedby\":null,\"updatedon\":null,\"is_deleted\":false}}}

从数据库中读取它时,如何将其转换为java的域?

我尝试使用Gson,但没有用! 数据数据=新的Gson()。fromJson(json,Data.class);

我还尝试使用Parser No USe :(请帮助我!

解决方法

尝试使用Jackson。这是Java的JSON解析库。

您可以这样做,例如:

Player ronaldo = new ObjectMapper().readValue(jsonString,Player.class);

播放器是您要将jsonString转换为的类。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...