2

我有一段代码试图输入双引号,我的代码如下,

resulted_value = "{series_name : \"" + final_resulted_series_name + "\",period_name: \"" + period_name + "\",period_final_value: \"" + period_final_value + "\"}";

这里结果 \ 每次都显示,我使用了 "\"+variable+\"" 和 @"""variable""" 但我没有得到想要的输出..任何帮助将不胜感激。

------------------------------------ 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\\\"}\""]

我想消除 \ 并希望 "" 中的每个值,以便可以在进一步的代码中读取和使用它。

4

3 回答 3

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"}

正如 Marc 所指出的,这可能不是构造 json 字符串的最佳方式。我建议使用 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;
}

您应该将您的对象标记为使用 aDataContract并且正在Serializable使用它。

[DataContract]
[Serializable]
于 2013-11-12T11:26:52.880 回答
2

如果您不确定,我可以推荐构建它的唯一方法是使用序列化程序。例如:

var obj = new {
    series_name = final_resulted_series_name,
    period_name,
    period_final_value 
};
var json = JsonConvert.SerializeObject(obj);

它产生(对于值abcdefghi分别):

{"series_name":"abc","period_name":"def","period_final_value":"ghi"}

如果你使用连接,那么它最终会咬你。您需要了解所有编码规则、边缘情况和极端情况才能手动执行此操作。

于 2013-11-12T11:27:54.543 回答
1
Console.WriteLine("You can use \"slashes\" to escape \" double quotes\"");
Console.WriteLine(@"Or use ""@"" before the string and double ""double quotes""");

输出:

您可以使用“斜杠”来转义“双引号”

或者在字符串前使用“@”和双“双引号”

请记住,如果您在调试器中检查字符串,您仍然会看到代码中出现的字符串(IE 包括转义斜杠或双双引号)。

于 2013-11-12T11:27:03.387 回答