0

我写了一个代码来消除双引号,如下所示,

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

也尝试使用@"""variable""" 但徒劳无功,我想消除 \ 斜线并希望我的每个值都在 "" 内。下面是我得到的结果。

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

下面是我编写的将字符串值序列化为 JSON 格式的代码。

modified_listofstrings.Add(resulted_value);
System.IO.File.WriteAllText(@"C:\Json\Json.json", jSearializer.Serialize(resulted_value));

我也试过这个方法

var obj = new
      {
        series_name = final_resulted_series_name,
        period_name,
        period_final_value
      };
System.IO.File.WriteAllText(@"C:\Json\Json.json",jSearializer.Serialize(obj));

这消除了反斜杠,但仅从第一个值开始,获得的输出是

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

我想从所有值中消除反斜杠。

下面是给我输出的代码,

if (xmlAttributeCollection_for_period != null)
                   {
                       var periodid = xmlAttributeCollection_for_period["periodid"];
                       xmlActions[j] = periodid.Value;
                       period_final_id = periodid.Value;
                       string period_name = Client.GetAttributeAsString(sessionId, periodid.Value, "name", "");

                       var action = xmlAttributeCollection_for_period["value"];
                       xmlActionsone[j] = action.Value;
                       period_final_value = action.Value;

                       values += final_resulted_series_name + ":" + period_name + ":" + period_final_value + ",";
                       string vals = values.Split(',')[1];
                       counts = values;
                       string[] periods = counts.Split(',');
                       Period1 = periods[j];
                       // string final_resulted_period_name = Client.GetAttributeAsString(sessionId, resulted_series_id, "name", "");
                       var obj = new
                       {
                           series_name = final_resulted_series_name,
                           period_name,
                           period_final_value
                       };

                       resulted_value = "{series_name : \"" + final_resulted_series_name + "\",period_name: \"" + period_name + "\",period_final_value: \"" + period_final_value + "\"}";
                       modified_listofstrings.Add(resulted_value);
                       System.IO.File.WriteAllText(@"C:\Json\Json.json", jSearializer.Serialize(resulted_value));
                   }

任何帮助将不胜感激...

4

1 回答 1

0

我将 obj 变量传递给列表如下

modified_listofstrings.Add(obj);

然后将列表序列化如下

jSearializer.Serialize(modified_listofstrings)

声明如下,

 List<object> modified_listofstrings = new List<object>();
 System.Web.Script.Serialization.JavaScriptSerializer jSearializer =
             new System.Web.Script.Serialization.JavaScriptSerializer();

它给出了所需的输出

于 2013-11-12T12:28:01.753 回答