我必须在vb.net将数据表转换为JSON格式。我已经为此编写了代码,但是,我想要另一种格式。 我添加了dll NewtonSoft.json并从数据库填充数据表, 我写的代码...... ''''我的dt充满了数据。我在Web服务中编写了代码
Dim jsonString As String = JsonConvert.SerializeObject(dt)
我得到的结果如 -
{"people":[{"ref":"108414","first_name":"George","last_name":"Buss","display_name":"George Buss","title":"Director of Experience & Education","organization_name":"Minnetrista","bio":"","address":"","city":null,"state":"IN","zipcode":"","country":"United States","phone_work":"","email":"","website":"","tags":"[Speaker,Attendees]","presenter_at":"[2307,8976]","organization_ref":"36441"}] }
但"tags"
值"[Speaker,Attendees]"
取代我需要的值
"tags":["Speaker","Attendees"]
和presenter_at也相同
从我的代码中我直接获取数据并将其转换为json。但为此,我认为要修改json数据,因为这些数据不是静态的,而是来自数据库。而且我会逐个改变。有谁知道解决方案,请帮助我。 谢谢你提前。
答案 0 :(得分:0)
public string DataTableToJSONWithJavaScriptSerializer(DataTable table)
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
Dictionary<string, object> childRow;
foreach (DataRow row in table.Rows)
{
childRow = new Dictionary<string, object>();
foreach (DataColumn col in table.Columns)
{
childRow.Add(col.ColumnName, row[col]);
}
parentRow.Add(childRow);
}
return jsSerializer.Serialize(parentRow);
}
此方法返回json字符串。 我希望它对你有帮助。