2

我必须将给定的 JSON 字符串读入 C# 对象。到目前为止一切都很好,但这个案子对我来说有点特别。JSON 字符串包含 2 个实体。一个是平面对象,第二个是列表,至少在 JSON 中是合乎逻辑的,但不是真正的。我希望你能帮助我找到解决这个问题的方法。

为了更好地解释它,我将向您展示我的 JSON 输入的一部分:

{
"game":{"GameMode":"1","IsNetworkMode":"1","NbMaxPlayer":"12","GameState":"1"},

"player_56":{"PlayerUserId":"137187","PlayerIALevel":"-1","PlayerObserver":"0"},
"player_7":{"PlayerUserId":"3440","PlayerIALevel":"-1","PlayerObserver":"0"}
}

我想将 Player 实体序列化为此类对象的集合。问题是它们并没有真正存储为 JSON 中的集合。它们的动态名称为“player_56”,并且数字不是像“1,2,3”这样的任何逻辑顺序。

目前我正在使用DataContractJsonSerializer这个任务。

[DataContract]
public class AlbReplay
{
    [DataMember(Name = "game")]
    public AlbGame Game { get; set; }
    [DataMember(Name = "player")]
    public List<AlbPlayer> Players { get; set; }
}

有什么建议么?

4

2 回答 2

1

将您的 JSON 分解为一组播放器对象。

player_8: {...}
player_99: {...}

players: [
    {id: 8 ...},
    {id: 99 ...}
]

如何破坏您的 JSON,RegEx 可能就足够了。

编辑这是使用正则表达式处理字符串的代码。做了一些假设:游戏和玩家对象中没有嵌入对象,玩家对象列表是 json 字符串的最后一部分。

        string json_test = @"{
""game"":{""GameMode"":""1"",""IsNetworkMode"":""1"",""NbMaxPlayer"":""12"",""GameState"":""1""},
""player_56"" : {""PlayerUserId"":""137187"",""PlayerIALevel"":""-1"",""PlayerObserver"":""0""},
""player_2"":  {""PlayerUserId"":""137187"",""PlayerIALevel"":""-1"",""PlayerObserver"":""0""}
}";
        json_test = new Regex(@"""player_(\d+)""\s*:\s*{").Replace(json_test, @"""player"" : {""Id"": $1,");
        Console.WriteLine("player_##:{...} -> player:{id: ##,..}");
        Console.WriteLine(json_test);
        json_test = new Regex(@"""player""\s*:\s*{").Replace(json_test, @"""players"" : [{", 1);
        json_test = new Regex(@"""player""\s*:\s*{").Replace(json_test, @"{");
        json_test = new Regex(@"}$").Replace(json_test, @"]}");
        Console.WriteLine("player:{...}, -> players: [{...},...]");
        Console.WriteLine(json_test);

与有关速度的所有考虑因素一样,您必须对其进行测试,如果我必须优化上述内容,那么将 Regex 对象设为静态和可重用将是我的第一步。

于 2013-06-18T16:11:54.137 回答
1

我不知道 的范围DataContractJsonSerializer,但是您可以在您的类上实现一个接口来定义如何解析 JSON。

但是,如果您能够使用JSON.Net

public class Player
{
    public int Id { get; set; }
    public int PlayerUserId { get; set; }
    public int PlayerIALevel { get; set; }
    public int PlayerObserver { get; set; }
}

然后你可以使用 Linq-To-JSON:

var data = @"{
""game"":{""GameMode"":""1"",""IsNetworkMode"":""1"",""NbMaxPlayer"":""12"",""GameState"":""1""},

""player_56"":{""PlayerUserId"":""137187"",""PlayerIALevel"":""-1"",""PlayerObserver"":""0""},
""player_7"":{""PlayerUserId"":""3440"",""PlayerIALevel"":""-1"",""PlayerObserver"":""0""}
}";

JObject o = JObject.Parse(data);

IEnumerable<Player> players =
    o.Children()
    .Where(p => ((JProperty)p).Name.StartsWith("player"))
    .Select(p =>
        new Player
        {
            Id = int.Parse(((JProperty)p).Name.Split('_')[1]),
            PlayerUserId = int.Parse((string)p.Children<JObject>().First()["PlayerUserId"]),
            PlayerIALevel = int.Parse((string)p.Children<JObject>().First()["PlayerIALevel"]),
            PlayerObserver = int.Parse((string)p.Children<JObject>().First()["PlayerObserver"]),
        });
于 2013-06-18T16:37:02.670 回答