2012-02-05 66 views
0

我正在尝试为FLOT绘制JSON文件,而且我几乎成功了! (好吧,除了一些例外:-)。我需要的数据格式吐出:以某种格式写入JSON数据

{ 
    "label": "ServiceOne", 
    "data": [[164, 35], [200, 35], 204, 35], [192, 84], [140, 54], [300, 66], [155, 110], [108, 101], [200, 94], [223, 99]] 
} 

相反,我得到这样的:

{ 
    "label:ServiceOne": [ 
    "data:", 
    "[164, 35]" 
    ] 
} 

格式化显然是错误的,它只是在结果拿起第一个值。

以下是我正在使用的代码。我知道问题出在哪里,我似乎无法解决它(我已经尝试了循环遍历结果集的几次迭代)。任何帮助将不胜感激!!!

using System; 
using System.Configuration; 
using System.Data.SqlClient; 
using System.IO; 
using Newtonsoft.Json; 


namespace ServiceInfo 
{ 

    public static class ServiceFlot 
    { 

     public static string servicename { get; set; } 
     public static string currentcount { get; set; } 
     public static string currentrating { get; set; } 

     public static void Main(string[] args) 
     { 
      try 
      { 

       SqlConnection cn = null; 
       cn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlConn"].ToString()); 
       cn.Open(); 
       SqlCommand cmd = new SqlCommand((@"SELECT 
         ServiceInfo.subid, 
         ServiceInfo.servicename, 
         ServiceDetails.currentcount, 
         ServiceDetails.currentrating 
         FROM 
          ServiceInfo (NOLOCK) 
         LEFT OUTER JOIN 
          ServiceDetails (NOLOCK) 
         ON 
          ServiceInfo.subid = ServiceDetails.subid 
         WHERE 
         ServiceDetails.lastupdated > DateADD(n, -600, GETDATE())            
         ORDER BY 
         ServiceInfo.servicename"), cn); 

       var myreader = cmd.ExecuteReader(); 

       while (myreader.Read()) 
       { 
        if (myreader["subid"] != null) 
        { 
         servicename = myreader["servicename"].ToString(); 
         currentcount = myreader["currentcount"].ToString(); 
         currentrating = myreader["currentrating"].ToString(); 
         getvars(servicename, currentcount, currentrating); 

         using (FileStream fs = File.Create(@"c:\" + servicename + ".json", 1024)) 
         using (StreamWriter sw = new StreamWriter(fs)) 

         using (JsonWriter jsonWriter = new JsonTextWriter(sw)) 
         { 
          jsonWriter.Formatting = Formatting.Indented; 

          jsonWriter.WriteStartObject(); 
          jsonWriter.WritePropertyName("label:" + servicename); 
          jsonWriter.WriteStartArray(); 

          jsonWriter.WriteValue("data:"); 
          for (int i = 0; i < 1; i++) 
          { 
           jsonWriter.WriteValue(currentcount + ", " + currentrating); 
          } 
          jsonWriter.WriteEnd(); 
          jsonWriter.WriteEndObject(); 
         } 


        } 

       } 

       cn.Close(); 
      } 
      catch (Exception e) 
      { 

       // Print error message 
       Console.WriteLine("Error encountered: " + e.Message); 

       // Exit the application with exit code 1 
       System.Environment.Exit(1); 

      } 
      finally 
      { 

      } 
     } 

    } 

} 
+1

任何特别的原因,为什么您使用的是JsonWriter,而不是直接更方便的API? – millimoose 2012-02-05 23:27:36

+0

我是JSON的新手,看起来很容易使用。随意推荐你认为可能会更好,只要我能达到相同的结果。 – mynameisneo 2012-02-05 23:33:43

回答

0

试试这个。

    using (JsonWriter jsonWriter = new JsonTextWriter(sw)) 
        { 
         jsonWriter.Formatting = Formatting.Indented; 

         jsonWriter.WriteStartObject(); 
         jsonWriter.WritePropertyName("label: \"servicename\""); 

         jsonWriter.WritePropertyName("data:"); 

         jsonWriter.WriteStartArray(); 

         for (int i = 0; i < 1; i++) 
         { 
          jsonWriter.WriteValue(currentcount + ", " + currentrating); 
         } 
         jsonWriter.WriteEnd(); 
         jsonWriter.WriteEndObject(); 
        } 
+0

jsonWriter.WriteProperty不编译..看起来不是一个可接受的方法。 – mynameisneo 2012-02-06 00:33:04

+0

你是对的它不存在。检查我编辑的答案。 – ShankarSangoli 2012-02-06 00:38:36

+0

仍然不会编译..看起来不像你可以有连续的WritePropertyName方法。 for循环仍然不遍历整个记录集:-(。我的键盘充满了头发。 – mynameisneo 2012-02-06 00:50:47