Reputation: 65
What I want to do is read each line of a text file individually and first find a certain string, and if that string is found read the integer in that line.
Here is how the strings look:
{
"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
}
Those "{" Brackets are in the text file.
Let's say I want to find the mouse color's R value, I would put each line into a string array and see if the string at index(i) contains "MouseColorR", how would I get the integer in that line??
Upvotes: 0
Views: 153
Reputation: 446
You can use Dynamic
to access a known attribute in any JSON object. Supposing that you are using JSON.NET as mentioned in other answers and comments, and text file name containing the Json object is stored in variable fileName
, the code would be like:
dynamic json = JsonConvert.DeserializeObject(File.ReadAllText(fileName));
var value = json.MouseColorR
Upvotes: 0
Reputation: 10824
As Stefan mentioned the best way for doing that is using JSON.NET:
var json = JsonConvert.DeserializeObject<Data>(str);
var value = json.MouseColorR;
str
is your json input string.
The 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; }
}
Also if you don't want to use JSON.NET, and you only want that 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;
}
Upvotes: 2