[英]Formating a string based on a list value
I am working in C# on a small program for PoE.我在 C# 工作,开发一个用于 PoE 的小程序。 Everything is working like it should except for one thing:
除了一件事,一切都按原样工作:
I am working with a json file, here is the part i need help with:我正在使用 json 文件,这是我需要帮助的部分:
"sockets": [{
"group": 0,
"attr": "I",
"sColour": "B"
}, {
"group": 0,
"attr": "D",
"sColour": "G"
}, {
"group": 1,
"attr": "D",
"sColour": "G"
}
]
Translated to the desired result, this part of my code need to return: BG G翻译成想要的结果,我这部分代码需要返回:BG G
sColour with the same group need to be linked by an hyphen. sColour 与同一组需要用连字符连接。
sColour with a different group are not linked with an hyphen.不同组的 sColour 不使用连字符链接。
I tried using StringBuilder with append, tried "string +=", tried foreach and for loops...我尝试将 StringBuilder 与 append 一起使用,尝试过“string +=”,尝试过 foreach 和 for 循环...
I just can't find a logic to return what i want with the data i have in the json.我只是找不到逻辑来返回我想要的 json 中的数据。
Here is the rest of my code:这是我的代码的 rest:
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();
}
}
}
I don't want someone to code it for me, i just need hints, thanks a lot!我不希望有人为我编写代码,我只需要提示,非常感谢!
EDIT:编辑:
I found a perfectly working solution, here is the code:我找到了一个完美的解决方案,这里是代码:
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}");
}
Try using CHOETL json reader(from nuggets).尝试使用 CHOETL json 阅读器(来自掘金)。 What you need from there is read the JSON and use a dynamic "foreach item".
您需要从那里阅读 JSON 并使用动态“foreach 项”。 If item.group == 0, then save the values to a list or a string getting item.sColour then format it as you want.
如果 item.group == 0,则将值保存到列表或获取 item.sColour 的字符串中,然后根据需要对其进行格式化。
Here's is the guide: https://github.com/Cinchoo/ChoETL这是指南: https://github.com/Cinchoo/ChoETL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.