从JSON格式修剪不需要的字符

时间:2013-11-18 10:38:49

标签: c# javascript json

我的代码如下,

 var obj2 = new
             {
                 layout,
                 floating,
                 bgColour,
                 align,
                 vertical_Align,
                 y,
                 x,
             };

             var obj1 = new { legend = new[] { obj2 } };

我的输出如下,

[
 {
  "legend":
   [
     {
       "layout":"vertical",
       "floating":"true",
       "bgColour":"#FFFFFF",
       "align":"right",
       "vertical_Align":"top",
       "y":"60","x":"-60"
     }
   ]
 }
]

我通过序列化方法得到的这种JSON格式如下,

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

我想修剪最外面的[]括号和第二个最外面的花括号来获得如下输出,

"legend":
   [
     {
       "layout":"vertical",
       "floating":"true",
       "bgColour":"#FFFFFF",
       "align":"right",
       "vertical_Align":"top",
       "y":"60","x":"-60"
     }
   ]

这是我进一步操作所需的格式, 任何人都可以为此建议解决方案,谢谢,

----------更新问题--------

以下是我的总输出

 [
   {

"legend":
     [  {"layout":"vertical","floating":"true","bgColour":"#FFFFFF","align":"right","vertical_Align   ":"top","y":"60","x":"-60"}
     ]
     },

  {
 "Series":
  [
     {"Data":[14,14,14,14,18,18,18,18,17,15,13,12]}
  ]
  },

  {"xAxis":
  [{"Categories":["Q1 / 2013","Q2 / 2013","Q3 / 2013","Q4 / 2013","Q1 / 2013","Q2 / 2013","Q3 / 2013","Q4 / 2013","Q1 / 2013","Q2 / 2013","Q3 / 2013","Q4 / 2013"]}
  ]
  }
 ]

我希望输出为

  [


"legend":
     [  {"layout":"vertical","floating":"true","bgColour":"#FFFFFF","align":"right","vertical_Align   ":"top","y":"60","x":"-60"}
     ]
     ,


 "Series":
  [
     {"Data":[14,14,14,14,18,18,18,18,17,15,13,12]}
  ]
  ,

  "xAxis":
  [{"Categories":["Q1 / 2013","Q2 / 2013","Q3 / 2013","Q4 / 2013","Q1 / 2013","Q2 / 2013","Q3 / 2013","Q4 / 2013","Q1 / 2013","Q2 / 2013","Q3 / 2013","Q4 / 2013"]}
  ]

  ]

-----更新问题---  如何解析从Web服务获得的JSON到javascript ... ???

2 个答案:

答案 0 :(得分:1)

你想要的是无效的JSON。大括号必须在那里。如果你直接序列化obj2(而不是像你在问题中显示的那样把它放在你的列表中),你可以摆脱最外面的方括号。

答案 1 :(得分:0)

在您提出问题时正确检查。您获得的JSON和您想要的格式都是无效的。转到jsonlint检查您的JSON是否有效。