【问题标题】:json serialization to rdf with java使用java将json序列化为rdf
【发布时间】:2013-02-19 15:20:26
【问题描述】:

我有一个这样的 json 对象:

[{
"id": 1,
"text": "Item 1",
"iconCls": "icon-ok",
"target": {
    "jQuery180016273543753015074": 16
},
"checked": false,
"state": "open",
"children": [{
    "id": 2,
    "text": "Item 1_1",
    "target": {
        "jQuery180016273543753015074": 15
    },
    "checked": false,
    "state": "open",
    "children": [{
        "id": 3,
        "text": "Item 1_1_1",
        "target": {
            "jQuery180016273543753015074": 14
        },
        "checked": false,
        "state": "open",
        "children": [{
            "id": 7,
            "text": "Item 1_1_1_1",
            "target": {
                "jQuery180016273543753015074": 13
            },
            "checked": false
        },
        {
            "id": 6,
            "text": "Item 1_1_1_2",
            "target": {
                "jQuery180016273543753015074": 12
            },
            "checked": false
        }]
    }]
},
{
    "id": 8,
    "text": "Item 1_1_2",
    "target": {
        "jQuery180016273543753015074": 11
    },
    "checked": false,
    "state": "open",
    "children": [{
        "id": 4,
        "text": "Item 1_1_2_1",
        "target": {
            "jQuery180016273543753015074": 10
        },
        "checked": false
    },
    {
        "id": 5,
        "text": "Item 1_1_2_2",
        "target": {
            "jQuery180016273543753015074": 9
        },
        "checked": false
    }]
},
{
    "id": 9,
    "text": "Item 1_1_3",
    "target": {
        "jQuery180016273543753015074": 17
    },
    "checked": false
}]
 }]

并且我需要使用 java 在 RDF 本体中对其进行序列化,从而将子级的层次结构保持为“子类”属性。任何人都可以建议一种有效的算法来解析 JSON 吗?我使用实验室的内部 java API 来处理本体,因此在这种情况下,算法而不是代码会更有帮助。

【问题讨论】:

    标签: java json serialization rdf


    【解决方案1】:

    您可以使用here 列出的任何库来解析您的 JSON 文档,并使用Jena 创建 RDF 三元组。

    您可能希望递归地遍历 JSON 文档,并为每个节点创建一个 RDF 节点,该节点的属性与该节点所拥有的 JSON 属性一样多。要表示子关系,您可以使用 rdfs:subClassOf,因此节点 2 将是节点 1 的 rdfs:subClassOf

    这是节点 1 和节点 2 如何在RDF/Turtle 中序列化的示例:

       @prefix : <http://other.example.org/ns#> .
    
       :node_1 rdf:type :Node;
        :text "Item 1";
        :iconCls 16;
        :target [
            :jQueryID "180016273543753015074";
            :number 11;
        ];
        :checked false;
        :state "open" .
    
      :node_2 rdf:type :Node;
        :text "Item 1";
        :iconCls 16;
        :target [
            :jQueryID "180016273543753015074";
            :number 11;
        ];
        :checked false;
        :state "open";
        rdfs:subClassOf :node_1 .
    

    您可能需要查看 Turtle 规范文档以了解它是如何构建的,无论如何它有点直观。请记住,有几种方法可以序列化 RDF 三元组,RDF/Turtle 是最易读的。如您所见,节点之间的子关系记录在节点 2 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多