我已经编写了一个C#程序来生成" Json" Eway Bill Uploading文件
我的Json格式是: - (当我在EwayBill上传时它不起作用)
请帮帮我,怎么做
[{"Supply Type":"Outward","Sub Type":"Export","Doc Type":"Tax Invoice","Doc No":"PK/18/0015","Doc Date":"16/02/2018 00:00:00","From_OtherPartyName":"KH Exports India Private Limited Glove Division","From_GSTIN":"33AAACR1714R1ZA","From_Address1":"142/1,Trunk Road","From_Address2":"Perumugai","From_Place":"Vellore","From_Pin Code":"632009","From_State":"Tamil Nadu","To_OtherPartyName":"K H EXPORTS INDIA PRIVATE LIMITED","To_GSTIN":"33AAACR1714R1ZA","To_Address1":"GLOVE DIVISION, GODOWN","To_Address2":"NEW NO. 24, KUMARAPPA STREET,","To_Place":"Chennai","To_Pin Code":"600003","To_State":"Tamil Nadu","Product":"FINISHED LEATHER GLOVES FOR LADIES","Description":"","HSN":"42032920","Unit":"PAIRS","Qty":"25","Assessable Value":"678","Tax Rate (S+C+I+Cess)":"9+9+0+0","CGST Amount":"6102","SGST Amount":"6102","IGST Amount":"0","CESS Amount":"0","Trans Mode":"Road","Distance (Km)":"115","Trans Name":"","Trans ID":"","Trans DocNo":"","Trans Date":"","Vehicle No":"TN23CB8274","Errors List":""}]
以下json工作正常(我从eway bill网站获得这个json)
{
"version":"1.0.0123",
"billLists":[{
"userGstin":"29BQSPA3829E124",
"supplyType":"O",
"subSupplyType":1,
"docType":"INV",
"docNo":"1234",
"docDate":"04/03/2017",
"fromGstin":"29BQSPA3829E124",
"fromTrdName":"HUKKERI PVT LTD",
"fromAddr1":"OLD AIRPORT ROAD",
"fromAddr2":"OLD AIRPORT ROAD",
"fromPlace":"BANGALORE",
"fromPincode":560090,
"fromStateCode":29,
"toGstin":"29AAACG0569P1Z3",
"toTrdName":"AMBUJA PVT LTD",
"toAddr1":"MG ROAD",
"toAddr2":"MG ROAD",
"toPlace":"BANGALORE",
"toPincode":560090,
"toStateCode":29,
"totalValue":678,
"cgstValue":6102,
"sgstValue":6102,
"igstValue":0,
"cessValue":0,
"transMode":1,
"transDistance":567,
"transporterName":"",
"transporterId":"",
"transDocNo":"",
"transDocDate":"",
"vehicleNo":"KA12KA1234",
"itemList":[{
"itemNo":1,
"productName":"STEEL",
"productDesc":"STEEL",
"hsnCode":26180000,
"quantity":0,
"qtyUnit":"KGS",
"taxableAmount":678,
"sgstRate":9,
"cgstRate":9,
"igstRate":0,
"cessRate":0
}
]
}
]
}
我在下面试过但是没有解决......请检查此代码......
public bool WriteJason(DataTable dt, string path)
{
try
{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, string>> rows = new List<Dictionary<string, string>>();
Dictionary<string, string> row = null;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, string>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName.Trim().ToString(), Convert.ToString(dr[col]));
}
rows.Add(row);
}
string jsonstring = serializer.Serialize(rows);
using (var file = new StreamWriter(path, false))
{
file.Write(jsonstring);
file.Close();
file.Dispose();
}
return true;
}
catch { return false; }
}
答案 0 :(得分:2)
正如@mcy在评论中提到的那样,您可以更轻松地使用Json.NET。
为了扩展这个想法,我个人会采取这种方法:
定义与您的Json结构匹配的模型:
private class JsonModel
{
public string Version { get; set; }
public IList<JsonBillModel> BillLists { get; set; } = new List<JsonBillModel>();
}
private class JsonBillModel
{
public string UserGstin { get; set; }
public string SupplyType { get; set; }
public int SubSupplyType { get; set; }
public string DocType { get; set; }
//...
}
创建模型的实例
var model = new JsonModel
{
Version = "1.0.0123",
BillLists =
{
new JsonBillModel
{
UserGstin = "29BQSPA3829E124",
SupplyType = "O",
SubSupplyType = 1,
DocType = "INV"
//...
}
}
};
使用Json.NET将模型序列化为Json:
var serializerSettings = new JsonSerializerSettings();
serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
var json = JsonConvert.SerializeObject(model, serializerSettings);
File.WriteAllText(path, json);
我们使用JsonSerializerSettings
合约解析程序创建CamelCasePropertyNamesContractResolver
实例的原因是我们的模型会在Json中生成PascalCase个键(因为我们的模型是用一个PascalCase格式。如果这听起来很混乱,请尝试省略ContractResolver-line并亲自查看结果。
旁注:您还可以使用Json.NET将json转换为模型的实例。
var model = JsonConvert.DeserializeObject<JsonModel>(json);