以特定格式编写JSON数据

时间:2012-02-05 23:25:38

标签: c# json

我试图为FLOT图表编写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 个答案:

答案 0 :(得分: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();
                    }