从JSON中只使用索引获取值?

huangapple go评论75阅读模式
英文:

how to get a value from json with just the index?

问题

I am only providing translations, so here is the translated text:

"我正在制作一个需要循环遍历Steam游戏的应用程序。阅读libraryfolder.vbf,我需要循环遍历并找到第一个值,并将其保存为字符串。

例如,它将记录:
730
4560
9200
11020
34010
34270

我已经在程序中使用System.Text.JSON,是否有办法通过System.Text.JSON循环遍历并仅获取第一个值,还是我需要采取不同的方法,因为vdf不使用冒号或逗号分隔值?"

英文:

Im making an app which needs to loop through steam games.
reading libraryfolder.vbf, i need to loop through and find the first value and save it as a string.

"libraryfolders"
{
	"0"
	{
		"path"		"D:\\Steam"
		"label"		""
		"contentid"		"-1387328137801257092942"
		"totalsize"		"0"
		"update_clean_bytes_tally"		"42563526469"
		"time_last_update_corruption"		"1663765126"
		"apps"
		{
			"730"		"31892201109"
			"4560"		"9665045969"
			"9200"		"22815860246"
			"11020"		"776953234"
			"34010"		"11967809445"
			"34270"		"1583765638"

for example, it would record:
730
4560
9200
11020
34010
34270

Im already using System.Text.JSON in the program, is there any way i could loop through and just get the first value using System.Text.JSON or would i need to do something different as vdf doesnt separate the values with colons or commas?

答案1

得分: 1

这不是JSON,而是由Valve开发的KeyValues格式。您可以在这里了解更多关于这种格式的信息:

https://developer.valvesoftware.com/wiki/KeyValues

关于将VDF文件转换为JSON的问题,已经有一些现有的stackoverflow问题,它们提到了已经开发的库,可以帮助读取VDF,这些库可以帮助您:

https://stackoverflow.com/questions/36231313/vdf-to-json-in-c-sharp

如果您想要一种非常快速而不需要任何外部库的方法来读取文件,我可能会使用正则表达式并执行类似以下的操作:

string pattern = "\"apps\"\\s+{\\s+(\"(\\d+)\"\\s+\"\\d+\"\\s+)+\\s+}";
string libraryPath = @"C:\Program Files (x86)\Steam\steamapps\libraryfolders.vdf";
string input = File.ReadAllText(libraryPath);

List<string> indexes = Regex.Matches(input, pattern, RegexOptions.Singleline)
   .Cast<Match>().ToList()
   .Select(m => m.Groups[2].Captures).ToList()
   .SelectMany(c => c.Cast<Capture>())
   .Select(c => c.Value).ToList();

foreach(string s in indexes)
{
    Debug.WriteLine(s);
}

在这里解释正则表达式的详细信息:
https://regex101.com/r/bQSt79/1

它基本上捕获了所有出现在0组中的 "apps" { },并在1组中重复捕获了大括号之间的数字对,但还捕获了数字对中左侧的数字在2组中。通常,重复捕获只会保留最后一次出现,但因为这是C#,我们仍然可以访问这些值。

代码的其余部分将每个匹配项、每个匹配项的第2组、每个组的捕获以及这些捕获的值放入一个字符串列表中。然后,通过foreach将这些字符串的值打印到日志中。

英文:

That is not JSON, that is the KeyValues format developed by Valve. You can read more about the format here:

https://developer.valvesoftware.com/wiki/KeyValues

There are existing stackoverflow questions regarding converting a VDF file to JSON, and they mention libraries already developed to help read VDF which can help you out.

https://stackoverflow.com/questions/36231313/vdf-to-json-in-c-sharp

If you want a very quick and dirty way to read the file without needing any external library I would probably use REGEX and do something like this:

string pattern = &quot;\&quot;apps\&quot;\\s+{\\s+(\&quot;(\\d+)\&quot;\\s+\&quot;\\d+\&quot;\\s+)+\\s+}&quot;;
string libraryPath = @&quot;C:\Program Files (x86)\Steam\steamapps\libraryfolders.vdf&quot;;
string input = File.ReadAllText(libraryPath);

List&lt;string&gt; indexes = Regex.Matches(input, pattern, RegexOptions.Singleline)
   .Cast&lt;Match&gt;().ToList()
   .Select(m =&gt; m.Groups[2].Captures).ToList()
   .SelectMany(c =&gt; c.Cast&lt;Capture&gt;())
   .Select(c =&gt; c.Value).ToList();

foreach(string s in indexes)
{
    Debug.WriteLine(s);
}

See the regular expression explaination here:
https://regex101.com/r/bQSt79/1

It basically captures all occurances of &quot;apps&quot; { } in the 0 group, and does a repeating capture of pairs of numbers inbetween the curely brackets in the 1 group, but also captures the left most number in the pair of numbers in the 2 group. Generally repeating captures will only keep the last occurance but because this is C# we can still access the values.

The rest of the code takes each match, the 2nd group of each match, the captures of each group, and the values of those captures, and puts them in a list of strings. Then a foreach will print the value of those strings to log.

huangapple
  • 本文由 发表于 2023年1月6日 11:06:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定