我的DataTable
如下所示:
id Descr value
CA-AB Descr1 3
CA-AC Descr2 4
CA-AD Descr3 8
使用以下代码,我能够生成以下json:
string callback = Newtonsoft.Json.JsonConvert.SerializeObject(table);
byte[] resultBytes = Encoding.UTF8.GetBytes(callback);
return new System.IO.MemoryStream(resultBytes);
结果如下:
[
{"id": "CA-AB", "Descr": "Descr1", "value": "3"},
{"id": "CA-AC", "Descr": "Descr2", "value": "4"},
{"id": "CA-AD", "Descr": "Descr3", "value": "8"}
]
但现在我需要更改格式,使其看起来像这样:
{
"CA-AB": {
"Descr": "Descr1",
"value": 3
},
"US-AK": {
"Descr": "Descr2",
"value": 4
},
"US-AZ": {
"Descr": "Descr3",
"value": 8
}
}
我试过这样的事情:
var returnData2 = new Json();
returnData2.map = "UpperTuple";
returnData2.areas = result.Tables[0];
string callback = JsonConvert.SerializeObject(returnData2);
byte[] resultBytes = Encoding.UTF8.GetBytes(callback);
return new System.IO.MemoryStream(resultBytes);
public class Json
{
// Case sensitive vvv to match your Json
public string map { get; set; }
public double zoomLevel { get; set; }
public DataTable areas { get; set; }
// you can have several constructor methods defined, I show the usage for each below.
public Json() { }
public Json(string countryMap, DataTable table, double zoom)
{
map = countryMap;
areas = table;
}
}
这会创建一个单独的"元组"在实际行之前,但它显然是静态的,所以这并没有多大帮助。
感谢任何帮助。
答案 0 :(得分:1)
使用JSON.NET(Newtonsoft.Json.Linq
)
var obj = new JObject(
table.Rows.Cast<DataRow>()
.Select(r => new JProperty(r["id"].ToString(),
new JObject(
new JProperty("Descr", r["Descr"].ToString()),
new JProperty("value", r["value"].ToString())
)
))
);
// Convert the JObject to a JSON string
var json = obj.ToString();
答案 1 :(得分:0)
正如此链接中所解释的:click here使用Json.Net,您可以执行类似下面的代码(此代码仅向您展示我如何实现您的json请求,并且它不遵循生产的最佳实践代码):
class Program
{
static void Main(string[] args)
{
DataSet dataset = new DataSet();
DataTable tableCAAB = new DataTable("CA-AB");
DataTable tableUSAK = new DataTable("US-AK");
DataTable tableUSAZ = new DataTable("US-AZ");
CreateColumns(tableCAAB);
CreateColumns(tableUSAK);
CreateColumns(tableUSAZ);
DataRow newRow = CreateRow(tableCAAB.NewRow(), "Descr1", 3);
tableCAAB.Rows.Add(newRow);
newRow = CreateRow(tableUSAK.NewRow(), "Descr2", 4);
tableUSAK.Rows.Add(newRow);
newRow = CreateRow(tableUSAZ.NewRow(), "Descr3", 8);
tableUSAZ.Rows.Add(newRow);
dataset.Tables.Add(tableCAAB);
dataset.Tables.Add(tableUSAK);
dataset.Tables.Add(tableUSAZ);
dataset.AcceptChanges();
string json = JsonConvert.SerializeObject(dataset, Formatting.Indented);
Console.WriteLine(json);
}
public static DataRow CreateRow(DataRow newRow, string value1, int value2)
{
newRow["Descr"] = value1;
newRow["value"] = value2;
return newRow;
}
public static void CreateColumns(DataTable table)
{
table.Columns.Add("Descr");
table.Columns.Add("value");
}
}
预期结果将是:
{
"CA-AB": [
{
"Descr": "Descr1",
"value": "3"
}
],
"US-AK": [
{
"Descr": "Descr2",
"value": "4"
}
],
"US-AZ": [
{
"Descr": "Descr3",
"value": "8"
}
]
}
希望有所帮助!祝你好运!
答案 2 :(得分:0)
您可以借助ExpandoObject
将数据表转换为合适的格式见下面的例子:
var expObject = (IDictionary<string, object>)new ExpandoObject();
foreach (var kv in dt.Rows.Cast<dynamic>()
.Select(
r =>
{
var kv = new KeyValuePair<string, object>(
r["id"],
new {Descr = r["Descr"], value = r["value"]});
return kv;
}))
{
expObject.Add(kv.Key, kv.Value);
}
var jsonString = JsonConvert.SerializeObject(expObject, Formatting.Indented);
Console.WriteLine(jsonString);
在DataTable中提供测试数据,这将打印
{
"CA-AB": {
"Descr": "Descr1",
"value": 3
},
"CA-AC": {
"Descr": "Descr2",
"value": 4
},
"CA-AD": {
"Descr": "Descr3",
"value": 8
}
}