【问题标题】:A code example of serialization JSON to JSON-LD in Java?Java中将JSON序列化为JSON-LD的代码示例?
【发布时间】:2017-04-04 22:55:32
【问题描述】:

我需要将大量以JSON 形式表示的数据转换为JSON-LD。我想用thisJSON-LDJava实现来做这个,但是我不明白,我该怎么办。

看了几个教程,在网上看了一些关于JSON-LD的东西,但是最简单的一个不懂:如何将JSON转换成JSON-LD

例如,我在 JSON 中有这段代码:

{
   "name" : "Andreas",
   "age" : 20,
   "profession" : "student",
   "personalWebsite" : "example.com"
}

我现在该怎么办? context.add("example.com") 之类的东西?

InputStream inputStream = new FileInputStream("C:\Users\Username\input.json");
Object jsonObject = JsonUtils.fromInputStream(inputStream);
Map context = new HashMap();
JsonLdOptions options = new JsonLdOptions();

【问题讨论】:

    标签: java json json-ld


    【解决方案1】:

    为您的数据添加一个“@context”条目。

    {
       "name" : "Andreas",
       "age" : 20,
       "profession" : "student",
       "personalWebsite" : "example.com",
       "@context":"http://schema.org/"
    }
    

    您的 JSON-LD 处理器将完成剩下的工作(从 0.9 版开始)

    作为测试,您可以将 json 转换为 rdf。

    package overflow.stack2449461;
    
    import java.io.ByteArrayInputStream;
    import java.io.InputStream;
    import java.io.StringWriter;
    import java.util.Collection;
    
    
    import org.eclipse.rdf4j.model.Statement;
    import org.eclipse.rdf4j.rio.RDFFormat;
    import org.eclipse.rdf4j.rio.RDFHandlerException;
    import org.eclipse.rdf4j.rio.RDFParser;
    import org.eclipse.rdf4j.rio.RDFWriter;
    import org.eclipse.rdf4j.rio.Rio;
    import org.eclipse.rdf4j.rio.helpers.StatementCollector;
    
    
    public class Test {
        @org.junit.Test
        public void testForYourCode() {
            String data = "{\"name\" : \"Andreas\",\"age\" : 20,\"profession\" : \"student\", \"personalWebsite\" : \"example.com\",\"@context\": \"http://schema.org/\"}";
            try (InputStream in = new ByteArrayInputStream(data.getBytes("utf-8"))) {
                String dataAsRdf = readRdfToString(in, RDFFormat.JSONLD, RDFFormat.NTRIPLES, "");
                System.out.println(dataAsRdf);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        /**
         * @param in
         *            a rdf input stream
         * @param inf
         *            the rdf format of the input stream
         * @param outf
         *            the output format
         * @param baseUrl
         *            usually the url of the resource
         * @return a string representation
         */
        public static String readRdfToString(InputStream in, RDFFormat inf, RDFFormat outf, String baseUrl) {
            Collection<Statement> myGraph = null;
            myGraph = readRdfToGraph(in, inf, baseUrl);
            return graphToString(myGraph, outf);
        }
    
        /**
         * @param inputStream
         *            an Input stream containing rdf data
         * @param inf
         *            the rdf format
         * @param baseUrl
         *            see sesame docu
         * @return a Graph representing the rdf in the input stream
         */
        public static Collection<Statement> readRdfToGraph(final InputStream inputStream, final RDFFormat inf,
                final String baseUrl) {
            try {
                final RDFParser rdfParser = Rio.createParser(inf);
                final StatementCollector collector = new StatementCollector();
                rdfParser.setRDFHandler(collector);
                rdfParser.parse(inputStream, baseUrl);
                return collector.getStatements();
            } catch (final Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        /**
         * Transforms a graph to a string.
         * 
         * @param myGraph
         *            a sesame rdf graph
         * @param outf
         *            the expected output format
         * @return a rdf string
         */
        public static String graphToString(Collection<Statement> myGraph, RDFFormat outf) {
            StringWriter out = new StringWriter();
            RDFWriter writer = Rio.createWriter(outf, out);
            try {
                writer.startRDF();
                for (Statement st : myGraph) {
                    writer.handleStatement(st);
                }
                writer.endRDF();
            } catch (RDFHandlerException e) {
                throw new RuntimeException(e);
            }
            return out.getBuffer().toString();
        }
    
    }
    

    使用 pom.xml

       <dependency>
            <groupId>org.eclipse.rdf4j</groupId>
            <artifactId>rdf4j-runtime</artifactId>
            <version>2.2</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>com.github.jsonld-java</groupId>
            <artifactId>jsonld-java</artifactId>
            <version>0.10.0</version>
        </dependency>
    

    它会将你的 json 字符串打印为 rdf NTRIPLES

    _:b0 <http://schema.org/age> "20"^^<http://www.w3.org/2001/XMLSchema#integer> .
    _:b0 <http://schema.org/name> "Andreas" .
    _:b0 <http://schema.org/personalWebsite> "example.com" .
    _:b0 <http://schema.org/profession> "student" .
    

    【讨论】:

      猜你喜欢
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多