如何在C#中反序列化包含n个边和节点的GraphQL响应。

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

How to deserialize GraphQL response for n no of edges and node in C#

问题

"Newtonsoft.Json.JsonSerializationException: 无法将当前 JSON 对象(例如,{"name":"value"})反序列化为类型 'System.Collections.Generic.IEnumerable1[ConsoleApp2.Edge1[ConsoleApp2.Location]]',因为该类型需要一个 JSON 数组(例如,[1,2,3])才能正确反序列化。"

如何将其反序列化为 Company 类?

英文:

I am trying to deserialize the GraphQL query result which may contain n no of edges and nodes into a class name "Company" but I am getting an error saying:

"Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IEnumerable1[ConsoleApp2.Edge1[ConsoleApp2.Location]]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.".

How can I deserialize it into Company Class?

public class Company
    {
        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("externalId")]
        public string ExternalId { get; set; }

        [JsonProperty("locations")]
        public IEnumerable<Edge<Location>> Locations { get; set; }
    }

    public class Edge<T>
    {
        [JsonProperty("edges")]
        public IEnumerable<Nodes<T>> Edges { get; set; }
    }

    public class Nodes<T>
    {
        [JsonProperty("node")]
        public T Node { get; set; }
    }

    public class Location
    {
        [JsonProperty("id")]
        public string Id { get; set; }
    }
    public class Program
    {
        static void Main(string[] args)
        {
            var json = "{\"name\":\"ABC Company\",\"id\":\"gid://shopify/Company/12345\",\"externalId\":\"12345\",\"locations\":{\"edges\":[{\"node\":{\"id\":\"gid://shopify/CompanyLocation/12345\"}}]}}";
            var defaultJObject = JsonConvert.DeserializeObject(json);

            // I am getting exception here
            var company = JsonConvert.DeserializeObject<Company>(json);
        }
    }

答案1

得分: 0

这个 bug 是因为你需要一个根类(在下面的 Data 类中)。以下是修复后的代码:

// 这个部分不需要翻译
var json = "{\"company\":{\"name\":\"Test\",\"id\":\"gid://shopify/Company/1234\",\"externalId\":\"123451\",\"locations\":{\"edges\":[{\"node\":{\"id\":\"gid://shopify/CompanyLocation/1234\"}}]}},\"userErrors\":[]}";

// 这个部分不需要翻译
Data data = JsonConvert.DeserializeObject<Data>(json);

public partial class Data
{
    [JsonProperty("company")]
    public Company Company { get; set; }

    [JsonProperty("userErrors")]
    public List<object> UserErrors { get; set; }
}

public partial class Company
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("externalId")]
    public long ExternalId { get; set; }

    [JsonProperty("locations")]
    public Locations<Node> Locations { get; set; }
}

public partial class Locations<T>
{
    [JsonProperty("edges")]
    public List<Edge<T>> Edges { get; set; }
}

public partial class Edge<T>
{
    [JsonProperty("node")]
    public T Node { get; set; }
}

public partial class Node
{
    [JsonProperty("id")]
    public string Id { get; set; }
}

IMHO 这更加灵活:

// 这个部分不需要翻译
Data<Node> data = JsonConvert.DeserializeObject<Data<Node>>(json);

public partial class Data<T> where T : class, new()
{
    [JsonProperty("company")]
    public Company<T> Company { get; set; }

    [JsonProperty("userErrors")]
    public List<object> UserErrors { get; set; }
}

public partial class Company<T>
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("externalId")]
    public long ExternalId { get; set; }

    [JsonProperty("locations")]
    public Locations<T> Locations { get; set; }
}

希望这对你有所帮助。

英文:

You have a bug, you need a root class ( class Data below)

this works for me

    var json = &quot;{\&quot;company\&quot;:{\&quot;name\&quot;:\&quot;Test\&quot;,\&quot;id\&quot;:\&quot;gid://shopify/Company/1234\&quot;,\&quot;externalId\&quot;:\&quot;123451\&quot;,\&quot;locations\&quot;:{\&quot;edges\&quot;:[{\&quot;node\&quot;:{\&quot;id\&quot;:\&quot;gid://shopify/CompanyLocation/1234\&quot;}}]}},\&quot;userErrors\&quot;:[]}&quot;;
	Data data = JsonConvert.DeserializeObject&lt;Data&gt;(json);

public partial class Data 
{
	[JsonProperty(&quot;company&quot;)]
	public Company Company { get; set; }

	[JsonProperty(&quot;userErrors&quot;)]
	public List&lt;object&gt; UserErrors { get; set; }
}

public partial class Company
{
	[JsonProperty(&quot;name&quot;)]
	public string Name { get; set; }

	[JsonProperty(&quot;id&quot;)]
	public string Id { get; set; }

	[JsonProperty(&quot;externalId&quot;)]
	public long ExternalId { get; set; }

	[JsonProperty(&quot;locations&quot;)]
	public Locations&lt;Node&gt; Locations { get; set; }
}

public partial class Locations&lt;T&gt;
{
	[JsonProperty(&quot;edges&quot;)]
	public List&lt;Edge&lt;T&gt;&gt; Edges { get; set; }
}

public partial class Edge&lt;T&gt;
{
	[JsonProperty(&quot;node&quot;)]
	public T Node { get; set; }
}

public partial class Node
{
	[JsonProperty(&quot;id&quot;)]
	public string Id { get; set; }
}

IMHO this is more flexible

	Data&lt;Node&gt; data = JsonConvert.DeserializeObject&lt;Data&lt;Node&gt;&gt;(json);

public partial class Data&lt;T&gt; where T:class, new ()
{
	[JsonProperty(&quot;company&quot;)]
	public Company&lt;T&gt; Company { get; set; }

	[JsonProperty(&quot;userErrors&quot;)]
	public List&lt;object&gt; UserErrors { get; set; }
}

public partial class Company&lt;T&gt; 
{
	[JsonProperty(&quot;name&quot;)]
	public string Name { get; set; }

	[JsonProperty(&quot;id&quot;)]
	public string Id { get; set; }

	[JsonProperty(&quot;externalId&quot;)]
	public long ExternalId { get; set; }

	[JsonProperty(&quot;locations&quot;)]
	public Locations&lt;T&gt; Locations { get; set; }
}

huangapple
  • 本文由 发表于 2023年2月27日 00:01:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573223.html
匿名

发表评论

匿名网友

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

确定