【问题标题】:How to Read a Json from database (SQL) and convert it into a java class?如何从数据库(SQL)中读取 Json 并将其转换为 java 类?
【发布时间】:2020-12-02 09:07:52
【问题描述】:

我使用 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 但没有用! 数据数据 = new Gson().fromJson(json, Data.class);

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

【问题讨论】:

  • 我已经尝试过对象映射器,但它没有工作! @Marcin Krysiak

标签: java sql json database jsonconvert


【解决方案1】:

尝试使用杰克逊。它是 Java 的 JSON 解析库。

你可以这样做,例如:

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

Player 是您要将 jsonString 转换为的类。

【讨论】:

  • 现在我可以使用 Object Mapper 读取 json 感谢您的解决方案,但我有嵌套类,我现在想将此 json 转换为。你能帮忙吗?
  • @Neeharika 您可以使用 Jackson API 的 ObjectMapper 类将此对象转换为 JSON。有一个教程如何做到这一点:geeksforgeeks.org/…
猜你喜欢
  • 2010-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-09
  • 2017-06-10
  • 1970-01-01
  • 2021-10-05
  • 2012-09-13
相关资源
最近更新 更多