Reputation: 1
I have a json string which is constructed by the following code:
string path1 = "C:\\Program Files (x86)\\IMAGE\\model\\net.mat";
string path2 = "C:\\Program Files (x86)\\IMAGE\\png\\Lab.png";
string path3 = "D:\\temp\\";
string[] strs={path1 ,path2 ,path3};
string json = JsonConvert.SerializeObject(strs);
Console.WriteLine(json);
List<string> paths = JsonConvert.DeserializeObject<List<string>>(json);
Console.WriteLine(paths.Count);
and there is no error when I serialize it or deserialize it. The json string is as follows:
"[\"C:\\\\Program Files (x86)\\\\IMAGE\\\\model\\\\net.mat\",\"C:\\\\Program Files (x86)\\\\IMAGE\\\\png\\\\Lab.png\",\"D:\\\\temp\\\\\"]"
Then I pass the string JSON to a *.exe file and deserialize it. The string the exe received is as follow,which has changed after pass:
string json="[C:\\\\Program Files (x86)\\\\IMAGE\\\\model\\\\net.mat,C:\\\\Program Files
(x86)\\\\IMAGE\\\\png\\\\Lab.png,D:\\\\temp\\]";
Then when I use the follow code to deserialize it,error appear.The code is as follow:
List<string> paths = JsonConvert.DeserializeObject<List<string>>(json);
The error is:
Unhandled JsonReaderException:Unexpected character encountered while parsing value: C. Path '', line 1, position 2.
I wonder why this is the case. Thanks.
Upvotes: 0
Views: 791
Reputation: 2221
You have to put single quotes around the paths.
string json = "[ 'C:\\Program Files (x86)\\IMAGEDL\\model\\net-e-100.mat',C:\\Program Files(x86)\\IMAGEDL\\Labelpng\\Lab.png, D:\\temp\\ ]";
Upvotes: 1