从文本文件中的特定行读取数字

马修·莱曼(Matthew Leman)

我想做的是分别读取文本文件的每一行,然后首先找到一个特定的字符串,如果找到该字符串,则读取该行中的整数。

这是字符串的外观:

{
"SmartCursorToggle": true,
"MapEnabled": true,
"InvasionBarMode": 2,
"AutoSave": true,
"AutoPause": false,
"Language": 1,
"PlacementPreview": true,
"GoreVisualsAllowed": true,
"VolumeSound": 1.0,
"VolumeAmbient": 0.75,
"VolumeMusic": 0.75,
"KeyUp": "W",
"KeyDown": "S",
"KeyLeft": "A",
"KeyRight": "D",
"KeyJump": "Space",
"KeyThrowItem": "T",
"KeyInventory": "Escape",
"KeyQuickHeal": "H",
"KeyQuickMana": "J",
"KeyQuickBuff": "B",
"KeyUseHook": "E",
"KeyAutoSelect": "LeftShift",
"KeySmartCursor": "LeftControl",
"KeyMount": "R",
"KeyMapStyle": "Tab",
"KeyFullscreenMap": "M",
"KeyMapZoomIn": "Add",
"KeyMapZoomOut": "Subtract",
"KeyMapAlphaUp": "PageUp",
"KeyMapAlphaDown": "PageDown",
"Fullscreen": false,
"WindowMaximized": false,
"DisplayWidth": 800,
"DisplayHeight": 704,
"GraphicsQuality": 0,
"BackgroundEnabled": true,
"FrameSkip": true,
"LightingMode": 0,
"LightingThreads": 0,
"MouseColorR": 252,
"MouseColorG": 233,
"MouseColorB": 221,
"Parallax": 90.0,
"ShowItemText": true,
"LastLaunchedVersion": 155,
"ClientUUID": "7d49a838-d7db-4e74-8124-92552a429491642949429491609524294916095159563f7c",
"UseSmartCursorForCommonBlocks": false,
"UseSmartAxeAfterSmartPickaxe": false,
"UseSmartWallReplacement": true,
"DisableLeftShiftTrashCan": false,
"HighlightNewItems": true,
"HidePasswords": false,
"ThickMouseEdges": true,
"ThickMouseEdgesPackedColor": 4289397106,
"ReverseUpDownForArmorSetBonuses": false,
"CloudSavingDefault": false
}

这些“ {”括号在文本文件中。

假设我要查找鼠标颜色的R值,我将每一行放入一个字符串数组,然后查看index(i)处的字符串是否包含“ MouseColorR”,我如何获取该行中的整数?

西尔万·阿菲菲(Sirwan Afifi)

正如Stefan提到的那样,最好的方法是使用JSON.NET

var json = JsonConvert.DeserializeObject<Data>(str);
var value = json.MouseColorR;

str 是您的json输入字符串。

数据类:

public class Data
{
    public bool SmartCursorToggle { get; set; }
    public bool MapEnabled { get; set; }
    public int InvasionBarMode { get; set; }
    public bool AutoSave { get; set; }
    public bool AutoPause { get; set; }
    public int Language { get; set; }
    public bool PlacementPreview { get; set; }
    public bool GoreVisualsAllowed { get; set; }
    public float VolumeSound { get; set; }
    public float VolumeAmbient { get; set; }
    public float VolumeMusic { get; set; }
    public string KeyUp { get; set; }
    public string KeyDown { get; set; }
    public string KeyLeft { get; set; }
    public string KeyRight { get; set; }
    public string KeyJump { get; set; }
    public string KeyThrowItem { get; set; }
    public string KeyInventory { get; set; }
    public string KeyQuickHeal { get; set; }
    public string KeyQuickMana { get; set; }
    public string KeyQuickBuff { get; set; }
    public string KeyUseHook { get; set; }
    public string KeyAutoSelect { get; set; }
    public string KeySmartCursor { get; set; }
    public string KeyMount { get; set; }
    public string KeyMapStyle { get; set; }
    public string KeyFullscreenMap { get; set; }
    public string KeyMapZoomIn { get; set; }
    public string KeyMapZoomOut { get; set; }
    public string KeyMapAlphaUp { get; set; }
    public string KeyMapAlphaDown { get; set; }
    public bool Fullscreen { get; set; }
    public bool WindowMaximized { get; set; }
    public int DisplayWidth { get; set; }
    public int DisplayHeight { get; set; }
    public int GraphicsQuality { get; set; }
    public bool BackgroundEnabled { get; set; }
    public bool FrameSkip { get; set; }
    public int LightingMode { get; set; }
    public int LightingThreads { get; set; }
    public int MouseColorR { get; set; }
    public int MouseColorG { get; set; }
    public int MouseColorB { get; set; }
    public float Parallax { get; set; }
    public bool ShowItemText { get; set; }
    public int LastLaunchedVersion { get; set; }
    public string ClientUUID { get; set; }
    public bool UseSmartCursorForCommonBlocks { get; set; }
    public bool UseSmartAxeAfterSmartPickaxe { get; set; }
    public bool UseSmartWallReplacement { get; set; }
    public bool DisableLeftShiftTrashCan { get; set; }
    public bool HighlightNewItems { get; set; }
    public bool HidePasswords { get; set; }
    public bool ThickMouseEdges { get; set; }
    public long ThickMouseEdgesPackedColor { get; set; }
    public bool ReverseUpDownForArmorSetBonuses { get; set; }
    public bool CloudSavingDefault { get; set; }
}

另外,如果您不想使用JSON.NET,并且只想要该值,则可以使用Regex:

var regex = new Regex("'MouseColorR': (\\d{3})");
Match match = regex.Match(str);
if (match.Success)
{
    string v = match.Groups[1].Value;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章