对不起标题,老实说我不知道如何描述那里的问题。 无论如何,这是我的困境。我正在解析一个信息文件。它的“作者”和“依赖”部分是相似的。但是,信息文件的创建者可以选择以不同方式编写。它可以写成:
"authors" : [ "author1", "author2" ],
或:
"authors": ["author1", "author 2"],
我需要将两种风格转换为:
author1, author2
我正在尝试解析的文件如下所示:
-snip "authors": [ "author1", "author2", "author3" -snip
我成功解析了作者写的第二种方式。但是,当我尝试解析第一种方式时,我得到一个NullReference异常。例外情况如下:
System.NullReferenceException未被用户代码处理HResult = -2147467261 Message =对象引用未设置为实例 一个对象。 Source = RKs工具包StackTrace: 在C:\ Users \ Sam \ Desktop \ Programing \ C#Projects \ RK Mod Installer \ RK Mod中的RK_Tool_kit.ParseModInfo.getAuthors(字符串路径) Installer \ RK Mod Installer \ ParseModInfo.cs:第119行 在C:\ Users \ Sam \ Desktop \ Programing \ C#Projects \ RK Mod中的RK_Tool_kit.AddMods.txtbxAddMods_DragDrop(Object sender,DragEventArgs e) Installer \ RK Mod Installer \ RK Mod Installer \ AddMods.cs:第85行 在System.Windows.Forms.Control.OnDragDrop(DragEventArgs drgevent) 在System.Windows.Forms.Control.System.Windows.Forms.IDropTarget.OnDragDrop(DragEventArgs) drgEvent) 在System.Windows.Forms.DropTarget.System.Windows.Forms.UnsafeNativeMethods.IOleDropTarget.OleDrop(Object pDataObj,Int32 grfKeyState,POINTSTRUCT pt,Int32& pdwEffect)
的InnerException:
我在这里不知所措。非常感谢提前。这是我的代码:
public static string getAuthors(string path)
{
string line;
using (StreamReader sr = new StreamReader(path))
{
while (!sr.EndOfStream)
{
line = sr.ReadLine();
if (line.Contains("author"))
{
//This detects if it is the second way the authors can write it
if (line.Contains("[") && line.Contains("]"))
{
line = line.Replace("\"authors\": ", "").Replace("\"authors\" : ", "").Replace("\"authorList\": ", "").Replace("\"authorList\" : ", "").Replace("[", "").Replace("]", "").Replace("\"", "").TrimStart(toTrim);
int Place = line.LastIndexOf(",");
string comma = ",";
line = line.Remove(Place, comma.Length).Insert(Place, "");
return line;
}
//This means the the author wrote it the first way. And the string "line" just says '"authors": [' so we want to read the line again.
else
{
line = sr.ReadLine();
line = line.Replace("\"", "").TrimStart(toTrim);
for (int i = 0; i < 101; i++)
{
//This checks to see if there is only one author in the array. If there is a comma, we want to read another line because there is another author.
if (line.Contains(","))
{
//THIS RIGHT HERE throws the exception
line = line + sr.ReadLine().Replace("\"", "").TrimStart(toTrim);
}
else
{
break;
}
}
return line;
}
}
}
}
return "N/A";
}