C# 无法在C中反序列化JSON字符串#

C# 无法在C中反序列化JSON字符串#,c#,json,file-handling,json-deserialization,json-serialization,C#,Json,File Handling,Json Deserialization,Json Serialization,我有以下型号: public class InputFile { public string Path { get; set; } // absolute path of file public string Content { get; set; } // full content of the json file } 及 collectedInputPath是通过用户输入提供的根目录的路径。我必须检查根目录下的每个目录,并读取每个JSON文件。 我得到了这个错误: 如何

我有以下型号:

public class InputFile
{
    public string Path { get; set; } // absolute path of file

    public string Content { get; set; } // full content of the json file
}

collectedInputPath
是通过用户输入提供的根目录的路径。我必须检查根目录下的每个目录,并读取每个JSON文件。 我得到了这个错误:

如何更正此问题?

从您的代码:

body+=$“{{\'Path\':{filePath},\'Content\':{Content}},”

filePath
content
是字符串。因此,您缺少属性值周围的引号
。即使有引号,
filePath
也可能包含反斜杠
\
,必须对其进行JSON转义。此时,您的文件内容包含双引号
,JSON字符串再次无效

使用您的方法,您还创建了一个无效数组,因为您正在添加一个
,在您添加到字符串的每个对象之后,数组的最终字符串将看起来像以下
[{…},{…},]
,这是无效的JSON(可能有一些解析器接受尾随逗号,但严格来说是不允许的)

为什么你还要手工创建JSON字符串,只是为了在几行代码后再次解析它?直接填充你的模型

InputModel input = new InputModel {
  Files = new List<InputFile>()
};
foreach(string directory in directories) // Going through all directories
{                
    foreach (string filePath in Directory.GetFiles(directory)) // Adding every file to the Input Model List
    {
        string content = System.IO.File.ReadAllText(filePath);                    
        var inputfile = new InputFile {
          Content = content, 
          Path = filePath
        };
        input.Files.Add(inputfile);
    }
}
InputModel输入=新的InputModel{
文件=新列表()
};
foreach(目录中的字符串目录)//遍历所有目录
{                
foreach(Directory.GetFiles(Directory))中的字符串filePath//将每个文件添加到输入模型列表中
{
字符串内容=System.IO.File.ReadAllText(文件路径);
var inputfile=新的inputfile{
内容=内容,
路径=文件路径
};
input.Files.Add(inputfile);
}
}

在反序列化之前打印JSON字符串,以验证它是否符合预期。您需要将内容编码为JSON值。请参见:@HansKilian我正在将它们转换为我的JSON模型。我正在生成路径,提取内容,创建“正文”字符串格式为符合我的模型的json字符串文件,并尝试对其进行反序列化。不,所有文件都不是json,但这不应该是问题所在。@vernou谢谢mate!问题解决了!
public void Parse(string collectedInputPath) // Command Line Input
{
    List<string> directories = new List<string>();
    directories.Add(collectedInputPath);
    directories.AddRange(LoadSubDirs(collectedInputPath));
    InputModel input = new InputModel();
    string body = string.Empty;
    foreach(string directory in directories) // Going through all directories
    {                
        foreach (string filePath in Directory.GetFiles(directory)) // Adding every file to the Input Model List
        {
            string content = System.IO.File.ReadAllText(filePath);                    
            string fp = JsonConvert.SerializeObject(filePath);
            string jsonbody = JsonConvert.SerializeObject(content);                    
            body += $"{{\"Path\" : {filePath}, \"Content\": {content}}},";
        }
    }
    body += "]}";
    body = "{\"Files\":[" + body;                
    input = JsonConvert.DeserializeObject<InputModel>(body);
    Solve(input);
}

private List<string> LoadSubDirs(string dir)
{
    string[] subdirectoryEntries = Directory.GetDirectories(dir);
    List<string> subDirectoryList = new List<string>();
    foreach (string subDirectory in subdirectoryEntries)
    {
        subDirectoryList.Add(subDirectory);
        subDirectoryList.AddRange(LoadSubDirs(subDirectory));
    }

    return subDirectoryList;
}
InputModel input = new InputModel {
  Files = new List<InputFile>()
};
foreach(string directory in directories) // Going through all directories
{                
    foreach (string filePath in Directory.GetFiles(directory)) // Adding every file to the Input Model List
    {
        string content = System.IO.File.ReadAllText(filePath);                    
        var inputfile = new InputFile {
          Content = content, 
          Path = filePath
        };
        input.Files.Add(inputfile);
    }
}