我期待從CSV或數據庫創建JSON數組。我將如何格式化我的源數據,或者是否有任何人建議/鏈接教程以創建.NET Web服務或其他方法從CSV或數據庫中生成JSON?下面是一個示例數組。從CSV或數據庫生成JSON數組
例子:
{
section1: [
{
"id":1,
"title":mytitle1
}
],
{
section2: [
{
"id":2,
"title":mytitle2
}
], etc.....
我期待從CSV或數據庫創建JSON數組。我將如何格式化我的源數據,或者是否有任何人建議/鏈接教程以創建.NET Web服務或其他方法從CSV或數據庫中生成JSON?下面是一個示例數組。從CSV或數據庫生成JSON數組
例子:
{
section1: [
{
"id":1,
"title":mytitle1
}
],
{
section2: [
{
"id":2,
"title":mytitle2
}
], etc.....
功能GetImageToJson解析對象JSON :串; var Q,Q1:TSQLQuery; content,Liststr:String; NextRecord:Integer; begin content:='['; Liststr:=''; Q:= TSQLQuery.Create(nil); Q.Database:= dmDatabase.IBConnection; Q.SQL.Text:= 'SELECT ID,TB,NAME FROM V_IMAGES'; Q.打開; Q.First; Q.First; NextRecord:= 0;如果不是Q.Fields [0] .IsNull則開始 而不是Q.eof do begin if(NextRecord = 1)then Liststr:= Liststr +','; Liststr:= Liststr +'{「ID」:''+ Q.Fields [0] .AsString +'「,'+ '」TB「:」'+ Q.Fields [1] .AsString +'「,'+ ''NAME':''+ Q.Fields [2] .AsString + '「}'; NextRecord:= 1; Q.Next; 結束;
end;
Q.Free;
Q := TSQLQuery.Create(nil);
Q.Database := dmDatabase.IBConnection;
Q.SQL.Text :=
'SELECT * FROM sETUP';
Q.Open;
Q.First;
if not Q.Fields[0].IsNull then begin
while not Q.eof do begin
if (NextRecord =1) then
Liststr :=Liststr +',';
Liststr := Liststr + '{"'+Q.Fields[0].DisplayName+'" : "' +Q.Fields[0].AsString+'",'+
'"'+Q.Fields[1].DisplayName+'" : "' +Q.Fields[1].AsString+'",'+
'"'+Q.Fields[2].DisplayName+'" : "' +Q.Fields[2].AsString+'",'+
'"'+Q.Fields[3].DisplayName+'" : "' +Q.Fields[3].AsString+'",'+
'"'+Q.Fields[4].DisplayName+'" : "' +Q.Fields[4].AsString+'",'+
'"'+Q.Fields[5].DisplayName+'" : "' +Q.Fields[5].AsString+'",'+
'"'+Q.Fields[6].DisplayName+'" : "' +Q.Fields[6].AsString+'",'+
'"'+Q.Fields[7].DisplayName+'" : "' +Q.Fields[7].AsString+'",'+
'"TB":"setup"'+
'}';
NextRecord :=1;
Q.Next;
end;
end;
Q.Free;
content := content +Liststr+ ']';
result := content;
end;
我想我的CSV文件的轉換爲對象,然後用newtonsoft的JSON類
Check this out to learn how to transform CSV to object
Go here to get newtonsoft's json.net tools (can be done using nuget)