Read numbers from a specific line in a text file


Matthew Leman

What I want to do is to read each line of the text file separately, then first find a specific string, and if that string is found, read the integer in that line.

Here's what the string looks like:

{
"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
}

These "{" brackets are in the text file.

Suppose I want to find the R value of the mouse color, I put each row into an array of strings, and see if the string at index(i) contains "MouseColorR", how can I get the integer in that row?

Sirwan Afifi

As Stefan mentioned, the best way is to use JSON.NET :

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

stris your json input string.

Data 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; }
}

Alternatively, if you don't want to use JSON.NET, and just want the value, you can use Regex:

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

Related


Read numbers from a specific line in a text file

Matthew Leman What I want to do is to read each line of the text file separately, then first find a specific string, and if that string is found, read the integer in that line. Here's what the string looks like: { "SmartCursorToggle": true, "MapEnabled": true,

Read numbers from a specific line in a text file

Matthew Leman What I want to do is to read each line of the text file separately, then first find a specific string, and if that string is found, read the integer in that line. Here's what the string looks like: { "SmartCursorToggle": true, "MapEnabled": true,

Read specific line from text file in Java

Jon: What is the most efficient way to extract data for a specific line number from a text file? For example, if I use a scanner to analyze the file, do I first have to create an array whose length matches the total number of lines in the text file? If the tex

Read specific line from text file in Java

Lluis Martinez: Is there any way to read a specific line from a text file? in the API or Apache Commons. It's just like: String readLine(File file, int lineNumber) I agree it's simple to implement, but not very efficient especially when the file is large. Bo

Read specific line from text file in Java

Lluis Martinez: Is there any way to read a specific line from a text file? in the API or Apache Commons. It's just like: String readLine(File file, int lineNumber) I agree it's simple to implement, but not very efficient especially when the file is large. Bo

Read specific line from text file in Java

Jon What is the most efficient way to extract data for a specific line number from a text file? For example, if I use a scanner to analyze the file, do I first have to create an array whose length matches the total number of lines in the text file? If the text

Read text file from specific line

Heng How can I read a text file containing the following text in matlab? Port: P988 Site: Bournemouth Latitude: 50.71433 Longitude: -1.87486 Start Date: 01JUL2017-00.00.00 End Date: 31JUL2017-23.45.00

Read specific line from text file in Java

Jon: What is the most efficient way to extract data for a specific line number from a text file? For example, if I use a scanner to analyze the file, do I first have to create an array whose length matches the total number of lines in the text file? If the tex

Read specific line from text file in Java

Lluis Martinez: Is there any way to read a specific line from a text file? in the API or Apache Commons. It's just like: String readLine(File file, int lineNumber) I agree it's simple to implement, but not very efficient especially when the file is large. Bo

Read specific line from text file in Java

Jon: What is the most efficient way to extract data for a specific line number from a text file? For example, if I use a scanner to analyze the file, do I first have to create an array whose length matches the total number of lines in the text file? If the tex

Read only specific line numbers from a large file in Python?

killer cod I have a large file and I want to open it and read specific lines from it, I always know where the data I want is on which line, but I don't want to just read the whole file every time and it's ok for a specific line. Is there a way to only read spe

Read only specific line numbers from a large file in Python?

killer cod I have a large file and I want to open it and read specific lines from it, I always know where the data I want is on which line, but I don't want to just read the whole file every time and it's ok for a specific line. Is there a way to only read spe

Read only specific line numbers from a large file in Python?

killer cod I have a large file and I want to open it and read specific lines from it, I always know where the data I want is on which line, but I don't want to just read the whole file every time and it's ok for a specific line. Is there a way to only read spe

Read only specific line numbers from a large file in Python?

killer cod I have a large file and I want to open it and read specific lines from it, I always know where the data I want is on which line, but I don't want to just read the whole file every time and it's ok for a specific line. Is there a way to only read spe

Read only specific line numbers from a large file in Python?

killer cod I have a large file and I want to open it and read specific lines from it, I always know where the data I want is on which line, but I don't want to just read the whole file every time and it's ok for a specific line. Is there a way to only read spe

How to read specific line and text from text file

crystal string lot = "RU644276G01"; var year = "201" + lot.Substring(2, 1); var folder = @"\\sinsdn38.ap.infineon.com\ArchView\03_Reports\" + year + @"\" + lot.Substring(3, 2) + @"\" + lot.Substring(0,8) + @"\"; DirectoryInfo di = new DirectoryI

Javascript read specific text line from text file

value For a little background: the text is printed on the screen. Text is a storyline. Now, there are many different paragraphs that can be printed, whichever paragraph is currently being printed can be controlled by setting a variable equal to the next string

Read specific line from text file to variable in batch file

username In a batch file I'm writing, I need to read the last line of a 5 line text file into a variable. One way I can think of might be to make each line in the text file overwrite the previous line (since both file creation and lines are created by commands

Remove specific line numbers from text file using sed?

Justin Estiel I want to delete one or more specific line numbers from a file. How would I do this with sed? Brian Campbell If you want to delete lines 5 to 10 and 12: sed -e '5,10d;12d' file This will print the result to the screen. If you want to save the re

Remove specific line numbers from text file using sed?

Justin Estiel I want to delete one or more specific line numbers from a file. How would I do this with sed? Brian Campbell If you want to delete lines 5 to 10 and 12: sed -e '5,10d;12d' file This will print the result to the screen. If you want to save the re

Remove specific line numbers from text file using sed?

Justin Estiel I want to delete one or more specific line numbers from a file. How would I do this with sed? Brian Campbell If you want to delete lines 5 to 10 and 12: sed -e '5,10d;12d' file This will print the result to the screen. If you want to save the re

Remove specific line numbers from text file using sed?

Justin Estiel I want to delete one or more specific line numbers from a file. How would I do this with sed? Brian Campbell If you want to delete lines 5 to 10 and 12: sed -e '5,10d;12d' file This will print the result to the screen. If you want to save the re