1

I have a text file that contains data that looks like this:

{\"_clientId\":\"123\",\"_timestamp\":22548} {\"_clientId\":\"222\",\"_timestamp\":22590}

(they have escape characters already)

When I read the file using string[] lines = System.IO.File.ReadAllLines(@"datafile.txt"); for example, it stores the strings in lines as:

"{\\\"_clientId\\\":\\\"123\\\",\\\"_timestamp\\\":22548}" "{\\\"_clientId\":\\\"222\\\",\\\"_timestamp\\\":22590}"

My question is: Is there a way to store them in the string variable without the additional escape characters (store them as they appeared in the file)?

4

2 回答 2

5

The lines should be stored in your string[] exactly as they are entered in the file.

If you look at the lines in your debugger, the debugger will show the escapes for visualization purposes only. That is probably what you are seeing.

Edit:

After looking at your example in detail, the problem is that you have already escaped the quotes in your data file. The data file itself does not contain valid Json.

This should fix the problem:

string fixedLine = line.Replace("\\\"", "\"");

Then deserialize fixedLine.

于 2013-01-31T03:03:51.680 回答
0

You will need to deserialize the file using some Deserializer. There are many available, you can use JSON.NET or Servicestack.Text. Servicestack's serializer and deserializer are the fastest on the planet. Using one of these , you can get your JSON data without escape characters and can store it or pass to the methods.

于 2013-01-31T03:05:55.190 回答