首页 > 解决方案 > 根据列表值格式化字符串

问题描述

我正在使用 C# 开发一个用于 PoE 的小程序。除了一件事,一切都按原样工作:

我正在使用一个 json 文件,这是我需要帮助的部分:

"sockets": [{
                "group": 0,
                "attr": "I",
                "sColour": "B"
            }, {
                "group": 0,
                "attr": "D",
                "sColour": "G"
            }, {
                "group": 1,
                "attr": "D",
                "sColour": "G"
            }
        ]

翻译成想要的结果,我的这部分代码需要返回:BG G
sColour 同组需要用连字符连接。
不同组的 sColour 不使用连字符链接。

我尝试使用带有附加的StringBuilder,尝试了“字符串+ =”,尝试了foreach和for循环......
我只是找不到一个逻辑来返回我想要的json中的数据。

这是我的其余代码:

public static void GenItemText()
    {
        var files = Directory.GetFiles("tmp","*.txt");
        foreach (var f in files)
        {
            File.Delete(f);
        }
        Stash j = JsonConvert.DeserializeObject<Stash>(File.ReadAllText("stash.json"));
        List<string> prop = new List<string>();
        foreach (var i in j.Items)
        {
            if (i.Name != "")
            {
                prop.Add("Rarity: Rare");
                prop.Add($"{i.Name}");
                prop.Add($"{i.TypeLine}");
                prop.Add("--------");
                if (i.Properties != null && i.Properties.Count() > 0)
                {
                    foreach (var p in i.Properties)
                    {
                        if (p.Name == "Quality")
                        {
                            prop.Add($"Quality: {p.Values[0][0].String}");
                        }
                        if (p.Name == "Armour")
                        {
                            prop.Add($"Armour: {p.Values[0][0].String}");
                        }
                        if (p.Name == "Energy Shield")
                        {
                            prop.Add($"Energy Shield: {p.Values[0][0].String}");
                        }
                        if (p.Name == "Evasion Rating")
                        {
                            prop.Add($"Evasion Rating: {p.Values[0][0].String}");
                        }
                        if (p.Values.Count() == 0)
                        {
                            prop.Add($"{p.Name}");
                        }
                        if (p.Name == "Physical Damage")
                        {
                            prop.Add($"Physical Damage: {p.Values[0][0].String}");
                        }
                        if (p.Name == "Elemental Damage")
                        {
                            prop.Add($"Elemental Damage: {p.Values[0][0].String}");
                        }
                        if (p.Name == "Critical Strike Chance")
                        {
                            prop.Add($"Critical Strike Chance: {p.Values[0][0].String}");
                        }
                        if (p.Name == "Attacks per Second")
                        {
                            prop.Add($"Attacks per Second: {p.Values[0][0].String}");
                        }
                        if (p.Name == "Weapon Range")
                        {
                            prop.Add($"Weapon Range: {p.Values[0][0].String}");
                        }
                    }
                    prop.Add("--------");
                }
                prop.Add("Requirements:");
                if (i.Requirements != null)
                {
                    foreach (var r in i.Requirements)
                    {
                        if (r.Name == "Level")
                        {
                            prop.Add($"Level: {r.Values[0][0].String}");
                        }
                        if (r.Name == "Str")
                        {
                            prop.Add($"Str: {r.Values[0][0].String}");
                        }
                        if (r.Name == "Dex")
                        {
                            prop.Add($"Dex: {r.Values[0][0].String}");
                        }
                        if (r.Name == "Int")
                        {
                            prop.Add($"Int: {r.Values[0][0].String}");
                        }
                    }
                }
                prop.Add("--------");
                if (i.Sockets != null)
                {
                    // don't know what to do here, tried all i could think about...
                }

                msg.CMW($"{i.Name} - {i.TypeLine}",true,1);
                File.WriteAllLines($@"tmp\{i.Id}.txt", prop);
                prop.Clear();
            }
        }
    }

我不希望有人为我编写代码,我只需要提示,非常感谢!

编辑:
我找到了一个完美的解决方案,这里是代码:

if (i.Sockets != null)
                {
                    List<string> g0 = new List<string>();
                    List<string> g1 = new List<string>();
                    List<string> g2 = new List<string>();
                    List<string> g3 = new List<string>();
                    List<string> g4 = new List<string>();
                    List<string> g5 = new List<string>();
                    foreach (var s in i.Sockets)
                    {
                        if (s.Group == 0)
                        {
                            g0.Add(s.SColour.ToString());
                        }
                        if (s.Group == 1)
                        {
                            g1.Add(s.SColour.ToString());
                        }
                        if (s.Group == 2)
                        {
                            g2.Add(s.SColour.ToString());
                        }
                        if (s.Group == 3)
                        {
                            g3.Add(s.SColour.ToString());
                        }
                        if (s.Group == 4)
                        {
                            g4.Add(s.SColour.ToString());
                        }
                        if (s.Group == 5)
                        {
                            g5.Add(s.SColour.ToString());
                        }
                    }
                    string g0f = string.Join("-", g0);
                    string g1f = string.Join("-", g1);
                    string g2f = string.Join("-", g2);
                    string g3f = string.Join("-", g3);
                    string g4f = string.Join("-", g4);
                    string g5f = string.Join("-", g5);
                    prop.Add($"{g0f} {g1f} {g2f} {g3f} {g4f} {g5f}");
                }

标签: c#json

解决方案


尝试使用 CHOETL json 阅读器(来自掘金)。您需要从那里阅读 JSON 并使用动态“foreach 项”。如果 item.group == 0,则将值保存到列表或获取 item.sColour 的字符串中,然后根据需要对其进行格式化。

这是指南:https ://github.com/Cinchoo/ChoETL


推荐阅读