简体   繁体   English

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

[英]To format the string to convert it into JSON format

I have written a code to eliminate the double quotes which is as follows, 我已经编写了消除双引号的代码,如下所示:

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

Also tried with @"""variable""" but was futile, I want to eliminate the \\ slash and want my every value to be inside "". 也尝试使用@“”“”“”“”,但没有用,我想消除\\斜线,并希望我的每个值都在“”内。 Below is my obtained result. 以下是我获得的结果。

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

Below is the code that I write to serialize the string value into JSON format. 下面是我编写的将字符串值序列化为JSON格式的代码。

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

also I tried this method as well 我也尝试过这种方法

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

this eliminate backslash but only from first values and the output obtained is 这消除了反斜杠,但仅从第一个值开始,并且获得的输出为

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

I want to eliminate backslash from all the values. 我想从所有值中消除反斜杠。

Below is the code which gives me the output, 以下是为我提供输出的代码,

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

Any Help will be greatly appreciated... 任何帮助将不胜感激...

I passed the obj variable to list as follows 我将obj变量传递给列表,如下所示

modified_listofstrings.Add(obj);

and then serialized the list as below 然后序列化如下列表

jSearializer.Serialize(modified_listofstrings)

declaration were as below, 声明如下

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

and it gave the desired output 它给出了期望的输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM