Json反序列化对象崩溃

时间:2016-10-05 22:13:56

标签: c# .net json json.net

我正在尝试将此JSON文件反序列化为对象,但我无法使其正常工作。

Json:

"hotkeySets": {
     "Player 1": {
          "chatOff": [
                    {
                    "actionsetting": {
                            "action": "Togglelist"
                        },
                        "keysequence": "Ctrl+B"
                    },
                    {
                        "actionsetting": {
                            "action": "Copy"
                        },
                        "keysequence": "Ctrl+C"
                    },
                    {
                        "actionsetting": {
                            "useObject": 3308,
                            "useType": "SelectUseTarget"
                        },
                        "keysequence": "F10"
                    },
                ],
                "chatOn": [
                    {
                        "actionsetting": {
                            "action": "Select All"
                        },
                        "keysequence": "Ctrl+A"
                    },
                    {
                        "actionsetting": {
                            "chatText": "heal friend",
                            "sendAutomatically": true
                        },
                        "keysequence": "Ctrl+F1"
                    },
                    {
                        "actionsetting": {
                            "action": "Copy"
                        },
                        "keysequence": "Ctrl+C"
                    },
                ]
            },
            "Player 2": {
                "chatOff": [
                    {
                        "actionsetting": {
                            "action": "ToggleBattlelist"
                        },
                        "keysequence": "Ctrl+B"
                    },
                    {
                        "actionsetting": {
                            "action": "Copy"
                        },
                        "keysequence": "Ctrl+C"
                    },
            }
}

这是我用来反序列化它的代码:

JToken hotkeyConfig = JObject.Parse(json)
JToken hotkeysSet = hotkeyConfig.SelectToken("hotkeySets");

var sets = JsonConvert.DeserializeObject<Dictionary<string, JObject>>(hotkeysSet.ToString());

foreach (var hotkeySet in sets) {
    string hotkeySetName = hotkeySet.Key; // "Player 1" etc..
    List<Hotkey> hotkeys = new List<Hotkey>();

    foreach (var hotkey in hotkeySet.Value) {

        Hotkey htk = JsonConvert.DeserializeObject<Hotkey>(hotkey.Value.ToString());    
     }
 }

但我可以让Hotkey对象与Json Deserialize一起使用。我尝试了很多东西,这是我试过的最后一个代码:

public class Hotkey {

        [JsonProperty("actionsetting")]
        public ActionSetting actionsetting;

        [JsonProperty("keysequence")]
        public string Shortcut { get; set; }

        public class ActionSetting {
            public int useObject{ get; set; }

            public string chatText { get; set; }

            public string useType{ get; set; }

            public bool sendAutomatically{ get; set; }
        }
    }

它在“JsonConvert.DeserializeObject”行中崩溃,说格式无效,所以我猜我的类结构中可能缺少一些东西。

2 个答案:

答案 0 :(得分:0)

检查你的Json字符串,格式错误显示错误。在这里,我将为您发布的Json字符串提供正确的格式。

.uniq()

这是一个Json验证器,你可以测试它JsonValidator 我希望这能解决你的问题。

答案 1 :(得分:0)

首先,您的Json文件中存在错误。您没有关闭“播放器2”的“chatOff”数组。检查下面,我在最后一行之前关闭了它:

"Player 2": {
    "chatOff": [
        {
            "actionsetting": {
                "action": "ToggleBattlelist"
            },
            "keysequence": "Ctrl+B"
        },
        {
            "actionsetting": {
                "action": "Copy"
            },
            "keysequence": "Ctrl+C"
        },
    ]
}

其次,您映射到Hotkey的方法很好,但您无法将数组项直接映射到Hotkey类。作为替代方案,您可以映射新对象以表示您的播放器,然后将属性“chatOff”和“chatOn”映射到列表中。让你的Json类看起来像这样:

public class Player
{
    [JsonProperty("chatOff")]
    public List<Hotkey> ChatOff { get; set; }

    [JsonProperty("chatOn")]
    public List<Hotkey> ChatOn { get; set; }
}

public class Hotkey
{
    [JsonProperty("actionsetting")]
    public ActionSetting ActionSetting;

    [JsonProperty("keysequence")]
    public string Shortcut { get; set; }
}

public class ActionSetting
{
    [JsonProperty("useObject")]
    public int UseObject { get; set; }

    [JsonProperty("chatText")]
    public string ChatText { get; set; }

    [JsonProperty("useType")]
    public string UseType { get; set; }

    [JsonProperty("sendAutomatically")]
    public bool SendAutomatically { get; set; }
}

最后,让您的反序列化代码如下所示:

var hotkeyConfig = JObject.Parse(json);

var playersToken = hotkeyConfig.SelectToken("hotkeySets");

var playersDictionary = 
    JsonConvert.DeserializeObject<Dictionary<string, JObject>>(
        playersToken.ToString());

foreach (var playerEntry in playersDictionary)
{
    var player = JsonConvert.DeserializeObject<Player>(playerEntry.Value.ToString());
}