简体   繁体   English

如何将SPARQL查询结果作为JSON对象查看?

[英]How to view SPARQL query result as JSON objects?

I want to view SPARQL query results as JSON objects. 我想将SPARQL查询结果作为JSON对象查看。 For example, I have a RDF database where I have parents graph which includes children, relatives and their information. 例如,我有一个RDF数据库,其中有父母图,其中包括孩子,亲戚及其信息。 Is there a way to view them as JSON objects like 有没有办法将它们视为JSON对象,例如

"Parents": {
  "names": "some names"
   "children":[
     {"child1": {
       "name": name
     }}
   ]

.....

}

How can I achieve this? 我该如何实现? All suggestions are welcome. 欢迎所有建议。 Thanks 谢谢

SPARQL provides "application/sparql-results+json" as its document content-type for query solutions for consumption by applications that understand JSON. SPARQL提供“ application / sparql-results + json”作为其文档内容类型,用于查询解决方案,以供理解JSON的应用程序使用。

In recent times I've come to realize that this aspect of SPARQL is generally understood thereby creating artificial friction for "Web Developers" who work with tools that support JSON as the default content-type for structured data. 最近,我开始意识到SPARQL的这一方面已被普遍理解,从而为使用支持JSON作为结构化数据的默认内容类型的工具的“ Web开发人员”造成了人为的摩擦。

Anyway, we recently released a HTML5, CSS, and Javascript based Single Page Application that demonstrates what's possible with SPARQL when you put its "application/results+json" query solution content-type to use. 无论如何,我们最近发布了一个基于HTML5,CSS和Javascript的单页应用程序,该示例演示了在使用SPARQL的“ application / results + json”查询解决方案内容类型时可以实现的功能。 Naturally, it also provides a solution for understanding how to process JSON returned from SPARQL. 当然,它还提供了一种解决方案,用于了解如何处理从SPARQL返回的JSON。

How the form works. 表格如何运作。

在此处输入图片说明

Code Snippet regarding JSON object handling 有关JSON对象处理的代码段

/*
                    Dynamic Table for processing JSON Structured Data (via "application/sparql-results+json" document content type)
                    that enables INSERT to be handled via a 3-tuple subject, predicate, object graph (relation) while query results 
                    are handled via an N-Tuple structured table (relation).
                    */
                    if (data.results.bindings.length > 0){
                        var table = tabCheckTable("dbmsTableID", "fsTableID") ; // creates table for header
                        var header = table.createTHead(); // creates empty tHead
                        var headRow = header.insertRow(0); // inserts row into tHead
                        var bindings = data.results.bindings;
                        for (var col = 0; col < data.head.vars.length; col++) { // for each column
                            // console.log("col = " + col)
                            var headCell = headRow.insertCell(col); // inserts new cell at position i in thead
                            headCell.innerHTML = "<b>" + data.head.vars[col] + "</b>"; // adds bold text to thead cell
                            }
                        for (i in bindings) {
                            // console.log("i = " + i)
                            var curr = 0 ; // curr is used to keep track of correct cell position
                            var binding = bindings[i];
                            var bodyRow = table.insertRow(-1); // create new row
                            for (n in binding) {
                            //  console.log("n = " + n)
                            //  console.log("curr = " + curr)
                                var bodyCell = bodyRow.insertCell(curr); // create new cell in row
                                bodyCell.innerHTML = tableFormat(binding[n].value); // set value of cell
                                curr += 1 ;
                            }
                        }
                    }   
                    else{
                        throw new Error("No Data Returned");
                        console.log("No data returned by query");
                    }
                })
            } catch(e) {
                console.error('Query Failed:', e) ;
            }          

        }

Links 链接

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

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