如何在C#中为JToken(Newtonsoft)创建一个扩展?

huangapple go评论60阅读模式
英文:

How to make an extension for JToken (Newtonsoft) in C#?

问题

我有一些返回对象和集合的Web API,当反序列化时,我可以获得一个JToken,它可以是JObjectJArray。我想为JToken类型编写一个扩展,将JToken转换为JObjectJArray

我知道可以通过制作一个参数来选择两种类型之一,或者制作两种不同的方法,但我想制作一个通用的转换方法。

我试图如何实现这样的任务:

namespace App.Extensions
{
    public static class JsonExtensions
    {
        public static T To<T>(this JToken token) => (T)token;
    }
}

但是遇到了错误:

无法将类型"Newtonsoft.Json.Linq.JToken"转换为"T"。

也许还有更文明的解决方法?

英文:

I have some Web API that returns objects and collections, when deserialized I can get a JToken, which can be either a JObject or a JArray. I want to write an extension for the JToken type that will translate JToken to JObject or JArray.

I know that it is possible to make a parameter to select one of two types or to make two different methods, but I want to make one universal method for conversion.

How am I trying to implement such a task:

namespace App.Extensions
{
    public static class JsonExtensions
    {
        public static T To<T>(this JToken token) => (T)token;
    }
}

And getting an error:
> Unable to convert type "Newtonsoft.Json.Linq.JToken" in "T".

Maybe there are more civil methods of solution?

答案1

得分: 1

我给你做了一个转换成类的示例

```C#

void Main()
{
	string json = "{\"employees\":[{\"name\":\"Shyam\",\"email\":\"shyamjaiswal@gmail.com\"},{\"name\":\"Bob\",\"email\":\"bob32@gmail.com\"},{\"name\":\"Jai\",\"email\":\"jai87@gmail.com\"}]}";
	var jobj = JsonConvert.DeserializeObject<JToken>(json);
	jobj.To<Root>().Dump();
}

public static class JsonExtensions
{
	public static T To<T>(this JToken token) where T : class
		=> (T)token.ToObject(typeof(T));

}

public class Employee
{
	public string name { get; set; }
	public string email { get; set; }
}

public class Root
{
	public List<Employee> employees { get; set; }
}

如果你需要处理 Tokens - 使用 JContainer

public static class JsonExtensions
{
	public static T To<T>(this JToken token) where T : JContainer 
		=> (T)token.ToObject(typeof(T));

}

调用方式如下

jobj.To<JObject>().Dump();
英文:

i made you example to convert to class


void Main()
{
	string json = &quot;{\&quot;employees\&quot;:[{\&quot;name\&quot;:\&quot;Shyam\&quot;,\&quot;email\&quot;:\&quot;shyamjaiswal@gmail.com\&quot;},{\&quot;name\&quot;:\&quot;Bob\&quot;,\&quot;email\&quot;:\&quot;bob32@gmail.com\&quot;},{\&quot;name\&quot;:\&quot;Jai\&quot;,\&quot;email\&quot;:\&quot;jai87@gmail.com\&quot;}]}&quot;;
	var jobj = JsonConvert.DeserializeObject&lt;JToken&gt;(json);
	jobj.To&lt;Root&gt;().Dump();
}

public static class JsonExtensions
{
	public static T To&lt;T&gt;(this JToken token) where T : class
		=&gt; (T)token.ToObject(typeof(T));

}

public class Employee
{
	public string name { get; set; }
	public string email { get; set; }
}

public class Root
{
	public List&lt;Employee&gt; employees { get; set; }
}

如何在C#中为JToken(Newtonsoft)创建一个扩展?

if you need to work with Tokens - use JContainer

public static class JsonExtensions
{
	public static T To&lt;T&gt;(this JToken token) where T : JContainer 
		=&gt; (T)token.ToObject(typeof(T));

}

and call would be

jobj.To&lt;JObject&gt;().Dump();

如何在C#中为JToken(Newtonsoft)创建一个扩展?

huangapple
  • 本文由 发表于 2023年7月10日 19:16:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定