【发布时间】:2018-01-30 07:48:18
【问题描述】:
我在 args 参数中将 JSON 值从 windows 应用程序传递到控制台应用程序,并尝试在类对象中对其进行反序列化,但在执行以下操作时出现错误,
解析值时遇到意外字符:L. Path 'Line1', 第 1 行,第 8 位。
我正在使用 JSON.NET 库来执行此操作。下面是我的代码,
Windows 应用程序,
Address address = new Address() { Line1 = "Line 1", Line2 = "Line 2" };
string json = JsonConvert.SerializeObject(address);
Process compiler = new Process();
compiler.StartInfo.FileName = @"D:\Learning\Console\ValidateAddress\ValidateAddress\bin\Debug\ValidateAddress.exe";
compiler.StartInfo.Arguments = json;
compiler.StartInfo.UseShellExecute = false;
compiler.StartInfo.RedirectStandardOutput = true;
compiler.Start();
compiler.WaitForExit();
button1.Text = compiler.StandardOutput.ReadToEnd();
控制台应用程序,
Address m = JsonConvert.DeserializeObject<Address>(args[0]);
System.IO.File.WriteAllText(@"D:\path.txt", args[0]);
Console.WriteLine(args[0]);
在控制台应用程序中,args[0] 参数在文本文件中打印低于值,
{Line1:Line 1,Line2:Line 2}
在字符串值中,它没有得到双引号。 (第 1 行,而不是“第 1 行”)
在 Windows 应用程序中,我得到以下字符串,
"{Line1:Line 1,Line2:Line 2}\r\n"
在反序列化期间我需要在哪里更改控制台应用程序以使其正常工作?
【问题讨论】:
-
你不应该自己提供双引号吗?
-
您的意思是,来自 Windows 应用程序?你能分享一下 JSON 字符串的例子吗?
标签: c# asp.net c#-4.0 serialization json.net