从文本文件中的某一行读取一个数字

Read a number from a certain line in a text file

我想做的是单独读取文本文件的每一行,首先找到某个字符串,如果找到该字符串,则读取该行中的整数。

字符串如下所示:

{
"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 值,我会将每一行放入一个字符串数组中,然后查看索引 (i) 处的字符串是否包含 "MouseColorR",我如何获得整数那条线??

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

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

str 是您的 json 输入字符串。

数据class:

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,而只想使用该值,则可以使用正则表达式:

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

您可以使用 Dynamic 访问任何 JSON 对象中的已知属性。假设您正在使用其他答案和评论中提到的 JSON.NET,并且包含 Json 对象的文本文件名存储在变量 fileName 中,代码如下:

dynamic json = JsonConvert.DeserializeObject(File.ReadAllText(fileName));
var value = json.MouseColorR