简体   繁体   English

NET中如何将json转换为json-ld

[英]How do I convert json to json-ld in.Net

I am trying to convert json to json-ld. 我正在尝试将json转换为json-ld。 So far I have tried the json-ld.net liberary from nuget (it is part of nuget3): https://www.nuget.org/packages/json-ld.net/ 到目前为止,我已经尝试了nuget的json-ld.net库(它是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);

the context resource: 上下文资源:

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

My json is present and valid. 我的json存在且有效。 It comes from REST service. 它来自REST服务。 (response), and jtoken is populated. (响应),并填充jtoken。 However, sz only contains the context: 但是,sz仅包含上下文:

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

MXTires Microdata .NET is a good one. MXTires Microdata .NET是一个不错的选择。 Converts .Net classes to Schema.org structured data in form of JSON-LD. 以JSON-LD形式将.Net类转换为Schema.org结构化数据。

Nuget Link | Nuget链接 | Usage Link 使用链接

I think I framed the question incorrectly. 我认为我对这个问题的解释不正确。 POCO to JSON-LD can be accomplished easily with JsonLD.Entities on GitHub. 通过GitHub上的JsonLD.Entities可以轻松实现POCO到JSON-LD的实现。 If I start with POCO or convert JSON to POCO, then this works easily. 如果我以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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM