Java序列化 json序列化,Java中序列化JSON到JSON-LD的代码示例?

I have a task and though I need to convert a some mass of data represented in the form of JSON to JSON-LD. I want to do this with this JSON-LD Java implementation, but I don't understand, what should I do.

I have watched tutorials on the website and read something about JSON-LD on the Internet, but do not understand the simplest thing yet: how do I convert JSON to JSON LD?

For example, i have this code in JSON:

{

"name" : "Andreas",

"age" : 20,

"profession" : "student",

"personalWebsite" : "example.com"

}

What should I do then? Something like context.add("example.com")?

InputStream inputStream = new FileInputStream("C:\Users\Albert\input.json");

Object jsonObject = JsonUtils.fromInputStream(inputStream);

Map context = new HashMap();

JsonLdOptions options = new JsonLdOptions();

解决方案

Add a '@context' entry to your data.

{

"name" : "Andreas",

"age" : 20,

"profession" : "student",

"personalWebsite" : "example.com",

"@context":"http://schema.org/"

}

Your JSON-LD Processor will do the rest (since version 0.9)

As test you can convert your json to 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 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 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 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();

}

}

With pom.xml

org.eclipse.rdf4j

rdf4j-runtime

2.2

pom

com.github.jsonld-java

jsonld-java

0.10.0

It will print your json string as rdf NTRIPLES

_:b0 "20"^^ .

_:b0 "Andreas" .

_:b0 "example.com" .

_:b0 "student" .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值