如何格式化我的SPARQL for JS?

时间:2017-08-01 10:13:51

标签: javascript json sparql

我试图使用此http://landregistry.data.gov.uk/app/qonsole而我特意尝试在邮政编码中使用'交易'选项。这是我的JS电话。

var query =[
"prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>",
"prefix skos: <http://www.w3.org/2004/02/skos/core#>",
"prefix lrcommon: <http://landregistry.data.gov.uk/def/common/>",
"prefix lrppi: <http://landregistry.data.gov.uk/def/ppi/>",
"prefix xsd: <http://www.w3.org/2001/XMLSchema#>",
"SELECT ?paon ?saon ?street ?town ?county ?postcode ?amount ?date ?category",
"WHERE {",
  "VALUES ?postcode {'PL6 8RU'^^xsd:string}",

  "?addr lrcommon:postcode ?postcode.",

  "?transx lrppi:propertyAddress ?addr ;",
          "lrppi:pricePaid ?amount ;",
          "lrppi:transactionDate ?date ;",
          "lrppi:transactionCategory/skos:prefLabel ?category.",

  "OPTIONAL {?addr lrcommon:county ?county}",
  "OPTIONAL {?addr lrcommon:paon ?paon}",
  "OPTIONAL {?addr lrcommon:saon ?saon}",
  "OPTIONAL {?addr lrcommon:street ?street}",
  "OPTIONAL {?addr lrcommon:town ?town}",
"}",
"ORDER BY ?amount"].join(" ");

var endpoint = 'http://landregistry.data.gov.uk/app/root/qonsole/query';

var queryUrl = encodeURI( endpoint+"?query="+query);

var fet = fetch(queryUrl)
.then((resp) => resp.json()) // Transform the data into json
  .then(function(data) {
    console.log(data)
    });

但我一直在接受:

  

&#34; Uncaught(在promise中)SyntaxError:意外的令牌&lt;在JSON中   位置0&#34;

.then((resp) => resp.json()) // Transform the data into json

.then(function(data) { //This line is the culprit according to console

    console.log(data)
    });

从环顾四周看,尝试使用.json()方法似乎就好了。

我相信&#34;&lt;&#34;来自查询中的前缀URL。我该如何解决?

1 个答案:

答案 0 :(得分:2)

Web UI screenshot

var endpoint = 'http://landregistry.data.gov.uk/landregistry/query';
var queryUrl = endpoint + '?query=' + encodeURIComponent(query);

Firefox console screenshot