我正在尝试将列表转换为json字符串。 json生成正常,但是当我尝试在JSON中生成一个数组时,它不会正确格式化。
我的列表已生成
List<string> invoicesList = new List<string>();
foreach (var invoice in theInvoices)
{
invoicesList.Add(invoice.InvoiceNumber);
invoicesList.Add(String.Format("{0:c}", invoice.GrandTotal));
invoicesList.Add(FieldTranslation.ToShortDate(invoice.Date.ToString()));
}
然后我将它添加到JSON
var info = new MobileAPIHelper.ClientPayrollInfo
{
GrossWages = String.Format("{0:c}", GrossWages),
InvoiceTotal = String.Format("{0:c}", invoiceTotal),
InvoiceDate = FieldTranslation.ToShortDate(invoiceDate.ToString()),
InvoiceList = invoicesList.ToArray()
};
输出最终只是一个长JSON字符串,包含列表中的所有内容
"InvoiceList":["SALES000000000006","$9,300.00","4/11/2016","SALES000000000008","$4,650.00","12/22/2015"]
我无法弄清楚如何让list / json格式化那样的invoicelist:
"InvoiceList":[{
"SALES000000000006","$9,300.00","4/11/2016"
},{
"SALES000000000008","$4,650.00","12/22/2015"
}]
答案 0 :(得分:1)
invoicesList不是包含这些值的对象列表,而是一个字符串列表。您需要创建一个充当
容器的类invoice.InvoiceNumber;
String.Format("{0:c}", invoice.GrandTotal);
invoice.Date.ToString());
这些领域。使invoices列出该类的列表,然后将其解析为json。你正在添加原始字符串。
答案 1 :(得分:1)
如果您将这些字符串作为对象。
import threading
import paramiko
import subprocess
def ssh_command(ip,user,passwd,command):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip,username = user, password = passwd)
ssh_session = client.get_transport().open_session()
if ssh_session.active:
ssh_session.exec_command(command)
print ssh_session.recv(1024)
return
ssh_command('192.168.100.301','GLZ','TripleBody','id')
然后有一个名为NewtonSoftJson的包,允许您从Collection转换。
转到包管理器控制台(单击visual studio中的视图菜单 - &gt;其他窗口 - &gt;包管理器控制台)
Install-Package Newtonsoft.Json
C#代码
var invoiceListString = JsonConvert.SerializeObject(invoicesList);
答案 2 :(得分:0)
我建议使用NewtonSoft.Json。
class PostModel(admin.ModelAdmin):
list_display = ('id', 'title', 'pub_date', 'course',)
search_fields = ('title', 'course__alias',)
form = PostModelForm
输出1
public class Invoice
{
public string InvoiceNumber { get; set; }
public DateTime InvoiceDate { get; set; }
public string InvoiceTotal { get; set; }
}
public void PrintJson()
{
List<Invoice> InvoiceList = new List<Invoice>();
var outputObject = new { InvoiceList };
InvoiceList.Add(new Invoice { InvoiceNumber = "SALES0000001", InvoiceDate = DateTime.UtcNow, InvoiceTotal = String.Format("{0:c}", "90000") });
InvoiceList.Add(new Invoice { InvoiceNumber = "SALES0000002", InvoiceDate = DateTime.UtcNow, InvoiceTotal = String.Format("{0:c}", "60000") });
var output1 = JsonConvert.SerializeObject(outputObject);
var output2 = JsonConvert.SerializeObject(InvoiceList);
}
输出2
{"InvoiceList":[{"InvoiceNumber":"SALES0000001","InvoiceDate":"2017-02-22T16:46:31.6933956Z","InvoiceTotal":"90000"},{"InvoiceNumber":"SALES0000002","InvoiceDate":"2017-02-22T16:46:31.6933956Z","InvoiceTotal":"60000"}]}
请注意您问题中的Json无效。
答案 3 :(得分:0)
正如Dispersia指出的那样,你的列表是一个大字符串列表,因此,json序列化程序的行为与请求一致 - 将这个大字符串列表转换为一个大的json字符串数组。
我手边的VS还没有测试下面的代码,但是请尝试将发票清单改为:
List<Tuple<String, String, String>> invoiceList = new List<>();
然后相应地添加元组:
foreach (var invoice in theInvoices)
{
Tuple<String, String, String> t = new Tuple<>(
invoice.InvoiceNumber,
String.Format("{0:c}", invoice.GrandTotal),
FieldTranslation.ToShortDate(invoice.Date.ToString()));
invoicesList.Add(t);
}
那会有帮助吗?