1

我从 GraphQL 端点收到此响应:

"container": {
                            "id": "X",
                            "containerId": "XYZ",
                            "metaData":"{\"postnummer\": \"9000 Aalborg\", \"ejendomsnr\": \"12345\", \"kundenr\": \"12345\", \"address\": \"\Ågade 5 C, 9000 Aalborg\", \"vej\": \"\Ågade\", \"nr\": \"5 C\"}",
                            "wasteFraction": {
                                "wasteCategory": "Combustion",
                                "wasteSubstance": "Gaseous",
                                "wasteTypes": {
                                    "edges": [
                                        {
                                            "node": {
                                                "name": "Residual waste"
                                            }
                                        }
                                    ]
                                }
                            }
                        }

我的问题是 metaData 属性,它本身是一个 json 字符串,GraphQLHttpClient/NewtonsoftJsonSerializer 似乎无法处理。

如果我将属性定义为具有正确属性的对象,则序列化失败。如果我像字符串一样定义元数据,那么我必须在之后手动反序列化它。

这可行,但我必须手动反序列化每个结果的字符串:

public class Container
{
    public string id { get; set; }
    public string containerId { get; set; }
    public string metaData { get; set; }
    public Wastefraction wasteFraction { get; set; }
}

public class MetaData
{
    public string postnummer { get; set; }

    public string husnr { get; set; }
    public string postdistrikt { get; set; }

    public string ejendomsnr { get; set; }
    public string kundenr { get; set; }
    public string address { get; set; }
    public string vej { get; set; }
    public string nr { get; set; }
}

这不起作用:

public class Container
{
    public string id { get; set; }
    public string containerId { get; set; }
    public MetaData metaData { get; set; }
    public Wastefraction wasteFraction { get; set; }
}

public class MetaData
{
    public string postnummer { get; set; }

    public string husnr { get; set; }
    public string postdistrikt { get; set; }

    public string ejendomsnr { get; set; }
    public string kundenr { get; set; }
    public string address { get; set; }
    public string vej { get; set; }
    public string nr { get; set; }
}

是否可以让 GraphQLHttpClient/NewtonsoftJsonSerializer 自动处理这个嵌套的 jsonstring?

4

0 回答 0