C# JSON字符串值保存时不带引号

C# JSON字符串值保存时不带引号,c#,.net,json.net,C#,.net,Json.net,我有两个节目。第一个程序使用JSON.Net创建一些JSON,然后启动第二个程序,将JSON传递给它。第二个程序使用WinForms中的SaveFileDialog将JSON保存到文件中。问题是JSON中的字符串值没有正确保存 例如,它可以节省时间 { projectName : MY_PROJECT_NAME } 应该是什么时候 { "projectName" : "MY_PROJECT_NAME" } 稍后,当我尝试反序列化JSON并转换为对象时,我得到了一个错误,但只使

我有两个节目。第一个程序使用JSON.Net创建一些JSON,然后启动第二个程序,将JSON传递给它。第二个程序使用WinForms中的
SaveFileDialog
将JSON保存到文件中。问题是JSON中的字符串值没有正确保存

例如,它可以节省时间

{
    projectName : MY_PROJECT_NAME
}
应该是什么时候

{
    "projectName" : "MY_PROJECT_NAME"
}
稍后,当我尝试反序列化JSON并转换为对象时,我得到了一个错误,但只使用字符串值

以下是保存文件的代码:

[STAThread]
static void Main(string[] args)
{
    string seriaizedData = args[0];
    Stream streamData;

    SaveFileDialog savefiledialog = new SaveFileDialog();
    savefiledialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/Bamboo Wall";
    savefiledialog.Filter = "bamboo files (*.bamboo)|*.bamboo|All files (*.*)|*.*";
    savefiledialog.FilterIndex = 1;
    savefiledialog.RestoreDirectory = true;

    if (savefiledialog.ShowDialog() == DialogResult.OK)
    {
        if ((streamData = savefiledialog.OpenFile()) != null)
        {
            byte[] buffer = Encoding.ASCII.GetBytes(seriaizedData);
            streamData.Write(buffer, 0, buffer.Length);
            streamData.Close();
        }
    }
}
以下是创建JSON的代码:

FloorModel grdData = GridData.gridData.gridDataClassList[GetActiveTabIndex()];
//How I get the object does not matter so much
string jsonObj = JsonConvert.SerializeObject(grdData);
print (jsonObj);

Process myProcess = new Process();
myProcess.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "Narnia.exe";
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.Arguments = jsonObj;
myProcess.Start();

我做错了什么?

问题是您在命令行上将JSON从一个程序传递到另一个程序。引号字符和空格对命令行解析器具有特殊意义,因此如果希望JSON字符串在命令行解析过程中完好无损,则需要明智地转义JSON字符串。另一个潜在的问题是命令行有一个长度限制,这取决于您运行的平台。因此,如果您试图传递的JSON很大,即使您能够正确地转义它,它也可能被截断。简而言之,我不推荐这种方法

相反,我会让您的第一个程序将JSON写入一个临时文件,然后通过命令行将临时文件的路径传递给第二个程序。然后,该程序可以将文件复制到用户指定的正确位置

比如说:

发送程序

FloorModel grdData = GridData.gridData.gridDataClassList[GetActiveTabIndex()];

string json = JsonConvert.SerializeObject(grdData);
string tempFile = Path.GetTempFileName();
File.WriteAllText(tempFile, json);

Process myProcess = new Process();
myProcess.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "Narnia.exe";
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.Arguments = '"' + tempFile + '"';
myProcess.Start();
string jsonTempFile = args[0];

try
{
    SaveFileDialog savefiledialog = new SaveFileDialog();
    savefiledialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/Bamboo Wall";
    savefiledialog.Filter = "bamboo files (*.bamboo)|*.bamboo|All files (*.*)|*.*";
    savefiledialog.FilterIndex = 1;
    savefiledialog.RestoreDirectory = true;

    if (savefiledialog.ShowDialog() == DialogResult.OK)
    {
        File.Copy(jsonTempFile, savefiledialog.FileName, overwrite: true);
    }
}
finally
{
    File.Delete(jsonTempFile);
}
接收节目

FloorModel grdData = GridData.gridData.gridDataClassList[GetActiveTabIndex()];

string json = JsonConvert.SerializeObject(grdData);
string tempFile = Path.GetTempFileName();
File.WriteAllText(tempFile, json);

Process myProcess = new Process();
myProcess.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "Narnia.exe";
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.Arguments = '"' + tempFile + '"';
myProcess.Start();
string jsonTempFile = args[0];

try
{
    SaveFileDialog savefiledialog = new SaveFileDialog();
    savefiledialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/Bamboo Wall";
    savefiledialog.Filter = "bamboo files (*.bamboo)|*.bamboo|All files (*.*)|*.*";
    savefiledialog.FilterIndex = 1;
    savefiledialog.RestoreDirectory = true;

    if (savefiledialog.ShowDialog() == DialogResult.OK)
    {
        File.Copy(jsonTempFile, savefiledialog.FileName, overwrite: true);
    }
}
finally
{
    File.Delete(jsonTempFile);
}

您显示的代码没有执行任何序列化。我非常怀疑这是否是一个JSON.NET问题。请提供一个。哦,非常感谢。我不知道我将在命令行中发送多少数据。你的方法要好得多。没问题;很高兴我能帮忙。