首页 > 解决方案 > Json 属性改变类型

问题描述

使用以下播放负载:

var playloads = new Dictionary<string, string>() {
{"FailMessage",
@"{
    ""success"": ""FALSE"",
    ""WsResult"": {
        ""result"": [],
        ""msg"": ""La r\u00e9f\u00e9rence ...""
    }
}"},
{"SuccessMessage", 
@"{
    ""success"": true,
    ""WsResult"": {
        ""result"": {
            ""FooNumber"": {
                ""Foo1"": {
                    ""Bar1"":""... il y a un probl\u00c3\u00a8me ."",
                    ""Bar2"":""bogus"",
                    ""Bar3"":""bogus""
                },
                ""Foo2"": {
                    ""Bar1"":""bogus"",
                    ""Bar2"":""bogus""
                },
            }
        },
        ""mssg"": """"
    }
}"} 
};

在此示例Dictionary<string, Result>中,当 Api 没有可提供的结果时,结果从 a 切换到空数组。我的成功请求的原始映射类是:

public class Foo1
{
    public string Bar1 { get; set; }
    public string Bar2 { get; set; }
    public string Bar3 { get; set; }
}

public class Foo2
{
    public string Bar1 { get; set; }
    public string Bar2 { get; set; }
}

public class Result
{
    public Foo1 Foo1 { get; set; }
    public Foo2 Foo2 { get; set; }
}

public class WsResult   {
    public Dictionary<string, Result> result { get; set; }
    public string msg { get; set; }
}

public class RootObject
{
    public string success { get; set; }
    public WsResult WsResult { get; set; }
}

虽然反序列化 Good message 有效,但 bad message 给出了错误:

var resultA = JsonConvert.DeserializeObject<RootObject>(playloads["SuccessMessage"]);
var resultB = JsonConvert.DeserializeObject<RootObject>(playloads["FailMessage"]);

无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 Dictionary<string, Result>

因此,我尝试添加具有此类型的属性,但未能使其使用相同的属性名称:

//[JsonProperty("result")]
public List<object> Failresult { get; set; }

我正在寻找一种简单的方法来处理这些情况。大局很简单:来自 API 的每个响应都有相同的结构,中间有一个棘手的属性结果,它有很大的倾向,永远不会返回 null 或负值(如 false 等),并且总是返回空数组。

Methode getSomething :结果是“某物”或空数组。
Methode PostSomething : 结果是 1 或空数组。
Methode PostSomething2 :结果为真或空数组。
Methode GetID :结果是 int ID 或空数组。如果没有找到。
ETC..

在 Json.net 中,有没有办法使用它们的类型在属性之间进行选择?

澄清:

这与 Json 的 C# 表示无关。它更多的是关于将相同的属性映射到不同的类型。但它不是那个的副本。一个空的 Nothing 数组不携带任何信息。所以没有必要将它映射到任何东西。但是您可以只 JsonIgnore 它,因为它可能是空数组以外的其他东西并携带所有信息。

标签: c#json

解决方案


使用这个优秀的站点JSON2CSHARP,我们可以看到失败的 json 生成了这些类:

public class ObjetWSResult
{
    public List<object> result { get; set; }
    public string msg { get; set; }
}

public class RootObject
{
    public string success { get; set; }
    public ObjetWSResult objetWSResult { get; set; }
}

成功生成 JSON 时:

public class Foo1
{
    public string Bar1 { get; set; }
    public string Bar2 { get; set; }
    public string Bar3 { get; set; }
}

public class Foo2
{
    public string Bar1 { get; set; }
    public string Bar2 { get; set; }
}

public class FooNumber
{
    public Foo1 Foo1 { get; set; }
    public Foo2 Foo2 { get; set; }
}

public class Result
{
    public FooNumber FooNumber { get; set; }
}

public class ObjetWSResult
{
    public Result result { get; set; }
    public string mssg { get; set; }
}

public class RootObject
{
    public bool success { get; set; }
    public ObjetWSResult objetWSResult { get; set; }
}

虽然RootObject是一致的,ObjectWSResult但不是,一个有List属性,另一个没有。因此,您不能使用一种机制来解析这些 JSON。

你可以做的是尝试解析 JSON 假设它是成功的,如果它产生错误,你会捕获它,然后你会以不同的方式解析它(到不同的类)。


推荐阅读