格式化具有特定格式的json文件 [英] Formatting a json file with specific format
问题描述
我创建了一个Windows应用程序,并将数据从数据库提取到datatable中。我正在使用Newtonsoft json.dll将数据转换为json格式。我得到的格式不是我需要的预期输出。我想要一定的特定格式。我怎么能在我的应用程序中实现这一点。我也试过替换方法无济于事。
我的应用程序代码::
< pre lang =c#> private void button1_Click(object sender,EventArgs e)
{
Open_Connection();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(select ID,TMaster中的Name,SQL_Conn);
da.Fill(dt);
DataTableToJSONWithJSONNet(dt);
}
公共字符串DataTableToJSONWithJSONNet(DataTable表)
{
string JSONString = string.Empty;
JSONString = JsonConvert.SerializeObject(table,Formatting.Indented);
// JSONString = JSONString.Replace(@\ r,');
返回JSONString;
}
从以上代码收到JSON O / PI:
[
{
ID:1,
名称:TP_G
},
{
ID:2 ,
名称:TP_S
},
{
ID:3,
名称:TP_Cl
},
{
ID:4,
名称:TPGM
}
]
我需要的json格式有点类似于\'id\'这样的方式:
{\'id\':\ '27GSPMH5711G1ZO \',\'名称'\\'':\ '072017 \',\'tt \':3782969.01,\'\\ n'cur_gt \ ':3782969.01,\'b2b \':[{\'ctin \':\ '27GSPMH5711G1ZO \',\'in'\\'':[{\'\\ n'\\'':'\ '9005 \',\'idt \':\'01-07-2017 \',\'val \':729248.16,\'pos\':\ '27 \ ',\'rchrg \ :\'N \',\'inv_typ \':\'R \',\'\\ t'atms \':[{\'num \':1,\'itm_det \\ \\':{\'rt \':5,\'s'='':10000,\'chart \':500,\'amt \':900}}]}]}] }
我尝试过:
尝试过字符串替换,尝试了jsonconvert类的各种方法但无济于事。
这不是一个完整的解决方案,只能解决JSON数据问题。 JSON [ ^ ]有一个非常具体的要求。
如上面的评论所述,所需的输出不是有效的格式。您可以在此处查看: JSON Formatter&验证器 [ ^ ]
更正的格式应为:
{ id: 27GSPMH5711G1ZO, name: 072017, gt: 3782969 。 01 , cur_gt: 3782969 。 01 , b2b:[{ ctin : 27GSPMH5711G1ZO, inv:[{< span class =code-string> inum: 9005, idt: 01-07-2017, val: 729248 。 16 , pos: 27, rchrg: N, inv_typ : R, itms:[{ num: 1 , itm_det:{ rt : 5 , txval:< span class =code-digit> 10000 , camt: 500 , samt: 900 }}]}]}]}
这是一个可读的版本:
{
id: 27GSPMH5711G1ZO,
name: 072017,
gt: 3782969 。 01 ,
cur_gt: 3782969 。 01 ,
< span class =code-string> b2b:[
{
ctin: 27GSPMH5711G1ZO ,
inv:[
{
inum: 9005,
idt: 01-07-2017,
val: 729248 。 16 ,
pos: 27,
rchrg: N,
inv_typ: R,
itms:[
{
num: 1 ,
itm_det:{
rt: 5 ,
txval: 10000 ,
camt: 500 ,
samt: 900
}
}
]
}
]
}
]
}
查看原始数据和所需的输出,似乎有很多缺少信息。
I have created a windows application and fetching data from database into datatable.I am using Newtonsoft json.dll for converting data into json format.The format I get is not the intended output I need.I want a certain specific format.How can I achieve this in my application.I also tried replace method to no avail.
My application code ::
<pre lang="c#"> private void button1_Click(object sender, EventArgs e) { Open_Connection(); DataTable dt = new DataTable(); SqlDataAdapter da = new SqlDataAdapter("select ID,Name from TMaster", SQL_Conn); da.Fill(dt); DataTableToJSONWithJSONNet(dt); } public string DataTableToJSONWithJSONNet(DataTable table) { string JSONString = string.Empty; JSONString = JsonConvert.SerializeObject(table,Formatting.Indented); // JSONString = JSONString.Replace(@"\r","'"); return JSONString; }
The JSON O/P I receive from the above code :
[ { "ID": 1, "Name": "TP_G" }, { "ID": 2, "Name": "TP_S" }, { "ID": 3, "Name": "TP_Cl" }, { "ID": 4, "Name": "TPGM" } ]
The json format I need somewhat similar like \'id\' in such a manner:
{ \'id\': \'27GSPMH5711G1ZO\', \'name\': \'072017\', \'gt\': 3782969.01, \'cur_gt\': 3782969.01, \'b2b\': [ { \'ctin\': \'27GSPMH5711G1ZO\', \'inv\': [ { \'inum\': \'9005\', \'idt\': \'01-07-2017\', \'val\': 729248.16, \'pos\': \'27\', \'rchrg\': \'N\', \'inv_typ\': \'R\', \'itms\': [ { \'num\': 1, \'itm_det\': { \'rt\': 5, \'txval\': 10000, \'camt\': 500, \'samt\': 900 } } ] } ] } ] }
What I have tried:
Tried string replace,tried jsonconvert class various methods but to no avail.解决方案This is not a full solution, only addressing the JSON data. JSON[^] has a very specific requirement.
As mentioned in the comments above, the desired output is not a valid format. You can check that here: JSON Formatter & Validator[^]
The corrected format should be:
{ "id": "27GSPMH5711G1ZO", "name": "072017", "gt": 3782969.01, "cur_gt": 3782969.01, "b2b": [ { "ctin": "27GSPMH5711G1ZO", "inv": [ { "inum": "9005", "idt": "01-07-2017", "val": 729248.16, "pos": "27", "rchrg": "N", "inv_typ": "R", "itms": [ { "num": 1, "itm_det": { "rt": 5, "txval": 10000, "camt": 500, "samt": 900 } } ] } ] } ] }
Here is a readable version:
{ "id":"27GSPMH5711G1ZO", "name":"072017", "gt":3782969.01, "cur_gt":3782969.01, "b2b":[ { "ctin":"27GSPMH5711G1ZO", "inv":[ { "inum":"9005", "idt":"01-07-2017", "val":729248.16, "pos":"27", "rchrg":"N", "inv_typ":"R", "itms":[ { "num":1, "itm_det":{ "rt":5, "txval":10000, "camt":500, "samt":900 } } ] } ] } ] }
Looking at the original data and the desired output, there appears to be a lot of information missing.
这篇关于格式化具有特定格式的json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!