Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从文本文件中的某一行读取数字_C#_String_Io_Int - Fatal编程技术网

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

C# 从文本文件中的某一行读取数字,c#,string,io,int,C#,String,Io,Int,我要做的是分别读取文本文件的每一行,然后首先找到某个字符串,如果找到该字符串,则读取该行中的整数 以下是字符串的外观: { "SmartCursorToggle": true, "MapEnabled": true, "InvasionBarMode": 2, "AutoSave": true, "AutoPause": false, "Language": 1, "PlacementPreview": true, "GoreVisualsAllowed": true, "VolumeSound

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

以下是字符串的外观:

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

你可以使用正则表达式来查找我该怎么做>你只想访问MouseCorr的值吗?这只是json文件的一部分吗?你可以使用优秀的json.NET库来解析这个json数据。例如,创建一个具有需要提取的属性的类,并使用JsonConvert.DeserializeObject方法。然后如何保存这些值要归档吗?
var regex = new Regex("'MouseColorR': (\\d{3})");
Match match = regex.Match(str);
if (match.Success)
{
    string v = match.Groups[1].Value;
}
dynamic json = JsonConvert.DeserializeObject(File.ReadAllText(fileName));
var value = json.MouseColorR