简体   繁体   English

如何从数据库(SQL)中读取 Json 并将其转换为 java class?

[英]How to Read a Json from database (SQL) and convert it into a java class?

I have created a column of type varchar(max) in the table in Database using for JSON Auto so that all the columns of that table get's stored as a JSON including Foreign keys also.我在数据库中的表中使用 JSON Auto 创建了一个类型为 varchar(max) 的列,以便该表的所有列都存储为 JSON 包括外键。

Now My Json I created looks like this:现在我创建的 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}}}

How can I convert it into a domain of java when reading it from the database?从数据库中读取时,如何将其转换为 java 的域?

I have tried using Gson but no use.我试过使用 Gson 但没有用。 Data data = new Gson(),fromJson(json. Data;class);数据数据 = new Gson(),fromJson(json.Data;class);

Also I tried using Parser No USe:( Please help me with this !我也尝试使用 Parser No USe :( 请帮帮我!

Try using Jackson.尝试使用 Jackson。 It's JSON parsing library for Java.它是 Java 的 JSON 解析库。

You can do it like this, for example:你可以这样做,例如:

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

Where Player is class you want to convert your jsonString to. Player是 class 您要将 jsonString 转换为的位置。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM