我有一段代码,我试图输入双引号,我的代码如下,
resulted_value = "{series_name : \"" + final_resulted_series_name + "\",period_name: \"" + period_name + "\",period_final_value: \"" + period_final_value + "\"}";
这里结果\每次都会显示,我使用了" \" +变量+ \""还有@"""变量"""但我没有得到理想的输出。非常感谢你的帮助。
------------------------ EDIT 1 -------------------- 我得到以下输出
["{series_name : \"Actual\",period_name:Q1 / 2013\",period_final_value:17\"}","\"{series_name : \\\"Actual\\\",period_name:Q1 / 2013\\\",period_final_value:17\\\"}\"","{series_name : \"Actual\",period_name:Q2 / 2013\",period_final_value:15\"}","\"{series_name : \\\"Actual\\\",period_name:Q2 / 2013\\\",period_final_value:15\\\"}\"","{series_name : \"Actual\",period_name:Q3 / 2013\",period_final_value:13\"}","\"{series_name : \\\"Actual\\\",period_name:Q3 / 2013\\\",period_final_value:13\\\"}\"","{series_name : \"Actual\",period_name:Q1 / 2013\",period_final_value:14.103\"}","\"{series_name : \\\"Actual\\\",period_name:Q1 / 2013\\\",period_final_value:14.103\\\"}\"","{series_name : \"Actual\",period_name:Q2 / 2013\",period_final_value:14.404499999999999\"}","\"{series_name : \\\"Actual\\\",period_name:Q2 / 2013\\\",period_final_value:14.404499999999999\\\"}\"","{series_name : \"Actual\",period_name:Q3 / 2013\",period_final_value:14.966999999999999\"}","\"{series_name : \\\"Actual\\\",period_name:Q3 / 2013\\\",period_final_value:14.966999999999999\\\"}\"","{series_name : \"Actual\",period_name:Q1 / 2013\",period_final_value:15.67\"}","\"{series_name : \\\"Actual\\\",period_name:Q1 / 2013\\\",period_final_value:15.67\\\"}\"","{series_name : \"Actual\",period_name:Q2 / 2013\",period_final_value:16.005\"}","\"{series_name : \\\"Actual\\\",period_name:Q2 / 2013\\\",period_final_value:16.005\\\"}\"","{series_name : \"Actual\",period_name:Q3 / 2013\",period_final_value:16.63\"}","\"{series_name : \\\"Actual\\\",period_name:Q3 / 2013\\\",period_final_value:16.63\\\"}\"","{series_name : \"Alarm\",period_name:Q1 / 2013\",period_final_value:17\"}","\"{series_name : \\\"Alarm\\\",period_name:Q1 / 2013\\\",period_final_value:17\\\"}\"","{series_name : \"Alarm\",period_name:Q2 / 2013\",period_final_value:15\"}","\"{series_name : \\\"Alarm\\\",period_name:Q2 / 2013\\\",period_final_value:15\\\"}\"","{series_name : \"Alarm\",period_name:Q3 / 2013\",period_final_value:13\"}","\"{series_name : \\\"Alarm\\\",period_name:Q3 / 2013\\\",period_final_value:13\\\"}\"","{series_name : \"Alarm\",period_name:Q1 / 2013\",period_final_value:14.103\"}","\"{series_name : \\\"Alarm\\\",period_name:Q1 / 2013\\\",period_final_value:14.103\\\"}\"","{series_name : \"Alarm\",period_name:Q2 / 2013\",period_final_value:14.404499999999999\"}","\"{series_name : \\\"Alarm\\\",period_name:Q2 / 2013\\\",period_final_value:14.404499999999999\\\"}\"","{series_name : \"Alarm\",period_name:Q3 / 2013\",period_final_value:14.966999999999999\"}","\"{series_name : \\\"Alarm\\\",period_name:Q3 / 2013\\\",period_final_value:14.966999999999999\\\"}\"","{series_name : \"Alarm\",period_name:Q1 / 2013\",period_final_value:15.67\"}","\"{series_name : \\\"Alarm\\\",period_name:Q1 / 2013\\\",period_final_value:15.67\\\"}\"","{series_name : \"Alarm\",period_name:Q2 / 2013\",period_final_value:16.005\"}","\"{series_name : \\\"Alarm\\\",period_name:Q2 / 2013\\\",period_final_value:16.005\\\"}\"","{series_name : \"Alarm\",period_name:Q3 / 2013\",period_final_value:16.63\"}","\"{series_name : \\\"Alarm\\\",period_name:Q3 / 2013\\\",period_final_value:16.63\\\"}\"","{series_name : \"Target\",period_name:Q1 / 2013\",period_final_value:17\"}","\"{series_name : \\\"Target\\\",period_name:Q1 / 2013\\\",period_final_value:17\\\"}\"","{series_name : \"Target\",period_name:Q2 / 2013\",period_final_value:15\"}","\"{series_name : \\\"Target\\\",period_name:Q2 / 2013\\\",period_final_value:15\\\"}\"","{series_name : \"Target\",period_name:Q3 / 2013\",period_final_value:13\"}","\"{series_name : \\\"Target\\\",period_name:Q3 / 2013\\\",period_final_value:13\\\"}\"","{series_name : \"Target\",period_name:Q1 / 2013\",period_final_value:14.103\"}","\"{series_name : \\\"Target\\\",period_name:Q1 / 2013\\\",period_final_value:14.103\\\"}\"","{series_name : \"Target\",period_name:Q2 / 2013\",period_final_value:14.404499999999999\"}","\"{series_name : \\\"Target\\\",period_name:Q2 / 2013\\\",period_final_value:14.404499999999999\\\"}\"","{series_name : \"Target\",period_name:Q3 / 2013\",period_final_value:14.966999999999999\"}","\"{series_name : \\\"Target\\\",period_name:Q3 / 2013\\\",period_final_value:14.966999999999999\\\"}\"","{series_name : \"Target\",period_name:Q1 / 2013\",period_final_value:15.67\"}","\"{series_name : \\\"Target\\\",period_name:Q1 / 2013\\\",period_final_value:15.67\\\"}\"","{series_name : \"Target\",period_name:Q2 / 2013\",period_final_value:16.005\"}","\"{series_name : \\\"Target\\\",period_name:Q2 / 2013\\\",period_final_value:16.005\\\"}\"","{series_name : \"Target\",period_name:Q3 / 2013\",period_final_value:16.63\"}","\"{series_name : \\\"Target\\\",period_name:Q3 / 2013\\\",period_final_value:16.63\\\"}\""]
我希望消除\并希望内部的所有价值""这样它就可以在进一步的代码中读取和使用。
答案 0 :(得分:2)
将输出写入控制台窗口,引号被转义。您可能正在调试器中查看,这将保存\
斜杠。
string final_resulted_series_name = "foo";
string period_name = "bar";
string period_final_value = "foobar";
string resulted_value = "{series_name : \"" + final_resulted_series_name + "\",period_name: \"" + period_name + "\",period_final_value: \"" + period_final_value + "\"}";
Console.Write(resulted_value);
给出:
{series_name:“foo”,period_name:“bar”,period_final_value:“foobar”}
这可能不是构建json字符串的最好方法,正如Marc所指出的那样。我建议使用json序列化器。
如果您使用的是json.net,则可以使用jsonConvert
方法:
JsonConvert.SerializeObject(obj);
如果没有,您可以使用以下内容:
public virtual string ToJson()
{
string json = null;
using (MemoryStream ms = new MemoryStream())
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(this.GetType());
ser.WriteObject(ms, this);
json = Encoding.UTF8.GetString(ms.ToArray());
}
return json;
}
您应该将对象标记为使用DataContract
并使用Serializable
来使用此对象。
[DataContract]
[Serializable]
答案 1 :(得分:2)
如果您不确定,我可以推荐来构建它的唯一方法是使用序列化程序。例如:
var obj = new {
series_name = final_resulted_series_name,
period_name,
period_final_value
};
var json = JsonConvert.SerializeObject(obj);
产生(分别为abc
,def
和ghi
值:
{"series_name":"abc","period_name":"def","period_final_value":"ghi"}
如果您使用串联,那么最终会咬你。您需要知道所有编码规则,边缘情况和角落情况才能手动执行此操作。
答案 2 :(得分:1)
Console.WriteLine("You can use \"slashes\" to escape \" double quotes\"");
Console.WriteLine(@"Or use ""@"" before the string and double ""double quotes""");
输出:
您可以使用“斜杠”来转义“双引号”
或在字符串前使用“@”并加倍“双引号”
请记住,如果您正在检查调试器中的字符串,您仍会看到代码中出现的字符串(I.E.包括转义斜杠或双引号)。