如何将json转换为json-ld in.Net

时间:2015-10-14 22:31:33

标签: c# json-ld

我正在尝试将json转换为json-ld。到目前为止,我已经尝试过来自nuget的json-ld.net liberary(它是nuget3的一部分):https://www.nuget.org/packages/json-ld.net/

var jtoken = JsonLD.Util.JSONUtils.FromString(response);
var options = new JsonLdOptions();
options.SetBase("http://json-ld.org/test-suite/tests/");
options.SetProduceGeneralizedRdf(true);
var context = JsonLD.Util.JSONUtils.FromString(Properties.Resources.jasonldcontext);
options.SetExpandContext((JObject)context);
var jtokenout = JsonLdProcessor.Compact(jtoken, context, options);
var sz = JSONUtils.ToString(jtokenout);

上下文资源:

{"@context": {
"ex": "http://example.org/",
"term1": {"@id": "ex:term1", "@type": "ex:datatype"},
"term2": {"@id": "ex:term2", "@type": "@id"}
}}

我的json存在且有效。它来自REST服务。 (响应),并填充了jtoken。但是,sz只包含上下文:

context":{"ex":"http://example.org/","term1":
{"@id":"ex:term1","@type":"ex:datatype"},"term2":
{"@id":"ex:term2","@type":"@id"}}}

2 个答案:

答案 0 :(得分:4)

MXTires Microdata .NET是一个很好的。将.Net类转换为JSON-LD形式的Schema.org结构化数据。

Nuget Link | Usage Link

答案 1 :(得分:0)

我认为我错误地构思了这个问题。使用GitHub上的JsonLD.Entities可以轻松完成POCO到JSON-LD。如果我从POCO开始或将JSON转换为POCO,那么这很容易。

var person = new Person
{
Id = new Uri("http://t-code.pl/#tomasz"),
Name = "Tomasz",
LastName = "Pluskiewicz"
};
var @context = JObject.Parse("{ '@context': 'http://example.org/context/Person' }");
var contextProvider = new StaticContextProvider();
contextProvider.SetContext(typeof(Person), @context);

// when
IEntitySerializer serializer = new EntitySerializer(contextProvider);
dynamic json = serializer.Serialize(person);