2015-08-15 93 views
0

我想要做的是逐個讀取文本文件的每一行,並首先找到某個字符串,如果找到該字符串,則讀取該行中的整數。從文本文件的某一行讀取一個數字

這裏是字符串的樣子:

{ 
"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」,如何獲得該行中的整數? ?

+0

您可以使用正則表達式查找它 –

+0

我該如何去做> –

+0

你只想訪問MouseColorR的值? –

回答

2

正如斯蒂芬提到的最好辦法做這件事是使用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,你只希望你可以使用正則表達式該值:

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

如何將值保存到文件? –

+0

看看這個:http://www.dotnetperls.com/streamwriter –

0

您可以使用Dynamic訪問任何JSON對象中的已知屬性。假設你正在使用JSON.NET在其他的答案和評論提及,並且包含JSON對象的文本文件名存儲在變量fileName,代碼會是這樣:

dynamic json = JsonConvert.DeserializeObject(File.ReadAllText(fileName)); 
var value = json.MouseColorR 
相關問題