简体   繁体   中英

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.

Now My Json I created looks like this:

{
    "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?

I have tried using Gson but no use. Data data = new Gson(),fromJson(json. Data;class);

Also I tried using Parser No USe:( Please help me with this !

Try using Jackson. It's JSON parsing library for Java.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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