我来自印度尼西亚的Joinnes。我想问一下将我的数据表转换为json对象。我正在使用带有Vb的asp.net。如何将我的数据表转换为Json像这样:
{
"awb": "81903054216",
"detail":
{
"shipped_date": "23-06-2016 08:24:00",
"delivered_date": "24-06-2016 10:54:00",
"services_code": "reguler",
"actual_amount": 33150,
"weight": 1000,
"final_status": 200,
"sender": {
"name": "ABCD",
"addr": "JL. ABCD",
"zipcode": 12345,
"city": "XXXX",
"geoloc": "xxx.bbb, xxx.aaa"
},
"receiver":{
"name": "BCDA",
"addr": "JL. BCDA",
"zipcode": 12345,
"city": "XXXX",
"geoloc": "xxx.bbb, xxx.aaa"
},
"driver":{
"id": "940184928",
"name": "driver A",
"phone": "12303523941",
"photo": ".com/potonya.jpg"
}
},
"history": [
{
"date_time": "20-JUN-2016 12:54:00",
"city_name": "XXXX",
"status": "manifested",
"status_code": 101,
"note": "something something",
"receiver": ""
},
{
"date_time": "21-JUN-2016 10:54:00",
"city_name": "XXXX",
"status": "Pickup Failed",
"status_code": 151,
"note": "something 1",
"receiver": ""
},
{
"date_time": "23-JUN-2016 12:54:00",
"city_name": "XXXX",
"status": "Shipping",
"status_code": 100,
"note": "something 2",
"receiver": ""
},
{
"date_time": "24-JUN-2016 10:54:00",
"city_name": "YYYY",
"status": "Delivered",
"status_code": 200,
"note": "something 3",
"receiver": "SETYAWAN"
}
]
}
提前感谢您的回复:)
答案 0 :(得分:1)
for VB
Public Function ConvertDataTabletoString() As String
Dim dt As New DataTable()
Using con As New SqlConnection("Data Source=SureshDasari;Initial Catalog=master;Integrated Security=true")
Using cmd As New SqlCommand("select title=City,lat=latitude,lng=longitude,description from LocationDetails", con)
con.Open()
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim rows As New List(Of Dictionary(Of String, Object))()
Dim row As Dictionary(Of String, Object)
For Each dr As DataRow In dt.Rows
row = New Dictionary(Of String, Object)()
For Each col As DataColumn In dt.Columns
row.Add(col.ColumnName, dr(col))
Next
rows.Add(row)
Next
Return serializer.Serialize(rows)
End Using
End Using
End Function
代表C#
public string ConvertDataTabletoString()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Initial Catalog=master;Integrated Security=true"))
{
using (SqlCommand cmd = new SqlCommand("select title=City,lat=latitude,lng=longitude,description from LocationDetails", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
}
}
答案 1 :(得分:0)
您可以通过执行此操作将数据表转换为JSON格式。
Public Function GetJson(ByVal dt As DataTable) As String
Return New JavaScriptSerializer().Serialize(From dr As DataRow In dt.Rows Select dt.Columns.Cast(Of DataColumn)().ToDictionary(Function(col) col.ColumnName, Function(col) dr(col)))
End Function
答案 2 :(得分:0)
这么简单
首先添加nuget包Newtonsoft.json 比在
项目中包含它using Newtonsoft.Json
比 像这样将数据表转换为json
return JsonConvert.SerializeObject(dt, Newtonsoft.Json.Formatting.Indented);