格式化字符串以将其转换为JSON格式

时间:2013-11-12 12:08:07

标签: c# regex json string

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

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

还尝试使用@“”“变量”“”但是徒劳无功,我想消除\斜杠并希望我的每个值都在里面“”。以下是我获得的结果。

["{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));
                   }

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

1 个答案:

答案 0 :(得分:0)

我将obj变量传递给list,如下所示

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();

它给出了所需的输出