将'[]'附加到json字符串C#

时间:2018-06-05 10:17:16

标签: c# json json.net

我使用D3雷达图表使用json文件中生成的json绘制图表。 我需要的是:

 [
    [
      {"Key": "Collaboration", "value": 0.22},
      {"Key": "People", "value": 0.41},
      {"Key": "Program/Project Complexity", "value": 0.26},
      {"Key": "Process", "value": 0.18},
      {"Key": "Tools", "value": 0.18}
    ]
]

然而,当我转换我的列表<>使用Newtonsoft给json我得到这种格式:

[
  {"Key": "Collaboration\r\n", "Value": 0.22},
  {"Key": "People\r\n","Value": 0.41},
  {"Key": "Program/Project Complexity\r\n","Value": 0.26},
  {"Key": "Process\r\n","Value": 0.18},
  {"Key": "Tools\r\n","Value": 0.18}
]

这是我将json转换并写入文件的代码:

var jsonResult = JsonConvert.SerializeObject(consolidatedData);
string path = Server.MapPath("~/JSON_Results/");
System.IO.File.WriteAllText(path + "data.json", jsonResult);

注意:在键值列表中的consolidatedData

有没有办法将'[]'附加到我的json文件?如果没有,请提出替代方案。

1 个答案:

答案 0 :(得分:0)

try:
    import tkinter as tk
    from tkinter import ttk
except ImportError:
    import Tkinter as tk
    import ttk

from tkcalendar import Calendar, DateEntry

def example1():
    def print_sel():
        print(cal.selection_get())

    top = tk.Toplevel(root)

    cal = Calendar(top,
                   font="Arial 14", selectmode='day',
                   cursor="hand1", year=2018, month=2, day=5)
    cal.pack(fill="both", expand=True)
    for i in range(6):
       cal._week_nbs[i].destroy()
    ttk.Button(top, text="ok", command=print_sel).pack()

def example2():
    top = tk.Toplevel(root)

    ttk.Label(top, text='Choose date').pack(padx=10, pady=10)

    cal = DateEntry(top, width=12, background='darkblue',
                    foreground='white', borderwidth=2)
    cal.pack(padx=10, pady=10)

root = tk.Tk()
s = ttk.Style(root)
s.theme_use('clam')

ttk.Button(root, text='Calendar', command=example1).pack(padx=10, pady=10)
ttk.Button(root, text='DateEntry', command=example2).pack(padx=10, pady=10)

root.mainloop()