Reputation: 53
I have a json object like this:
[{
"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
}]
}]
and I need to use java to serialize it in an RDF ontology that will keep the hierarchy of children as "subclass of" property. Can anybody suggest an efficient algorithm for parsing the JSON? I use a lab's internal java API for handling the ontology, so algorithm instead of code would be more helpful in this case.
Upvotes: 4
Views: 1409
Reputation: 16525
You can use any of the libraries listed here to parse your JSON document and Jena to create the RDF triples.
You probably want to traverse the JSON document recursively and for each node create an RDF node with as many attributes as JSON properties the node holds. To represent children relationship you can use rdfs:subClassOf
so node 2 would be an rdfs:subClassOf
of node 1.
This is an example of how node 1 and node 2 get serialized in 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 .
You might need to have a look at the Turtle spec document to understand how this gets build, it is kind of intuitive anyway. Bear in mind that there are several ways to serialize RDF triples, RDF/Turtle is the most readable. As you can see the children relation between the nodes gets recorded in node 2.
Upvotes: 4