您可以在https://github.com/dotnetrdf/dotnetrdf/wiki找到 dotNetRDF 文档。在https://github.com/dotnetrdf/dotnetrdf/wiki/UserGuide-Reading-RDF上有一些解析来自各种来源的 RDF 数据的示例。在 RDF 中,有只序列化单个图的语法和可以序列化多个图的语法 - JSON-LD 是后者之一,因此您还需要阅读有关 Store Readers 的部分。
以下示例均显示将数据加载到内存存储中。
如果您的内容源“行为良好”(发回正确类型的 Content-Type 标头,或者如果它是本地文件则具有预期的文件名后缀),那么加载数据可以像在- 内存图并调用其 Load 方法:
var store = new TripleStore();
# This is a convenience wrapper that simply invokes UriLoader.Load()
store.LoadFromUri(contentSourceUri)
注意:这使用了一种扩展方法(如https://github.com/dotnetrdf/dotnetrdf/wiki/UserGuide-Extension-Methods所述),它只是一个方便的包装:
# Create the store
var store = new TripleStore();
# UriLoader will make an HTTP request and parse the response,
# selecting the parser to be used based on the Content-Type header returned.
UriLoader.Load(store, contentSourceUri);
如果您正在从已经检索到的字符串进行解析,或者您需要明确说明要使用的解析器实例(例如,如果您想在创建解析器时将一些选项传递给解析器,则可能就是这种情况),那么你需要一个稍微详细一点的方法:
var store = new TripleStore();
# Create the parser (we can pass in options here if needed)
var parser = new JsonLdParser();
# Wrap the string content in a StringReader and pass the target graph and the reader
parser.Load(store, new StringReader(contentsFromUri));
当您特别提到 JSON-LD 时要注意的最后一件事。该解析器是符合 JSON-LD 1.0 的解析器,但它对 JSON-LD 1.1 的支持是基于早期的规范草案。我目前正在更新实现,并希望在几周内发布支持 JSON-LD 1.1 Proposed Recommendation 的新版本。