Convert a data row to a JSON object

后端 未结 4 1715
天涯浪人
天涯浪人 2020-12-11 16:08

I have a DataTable which has only a single row and looks like

   America              |  Africa               |     Japan     |   
   ----------         


        
4条回答
  •  隐瞒了意图╮
    2020-12-11 16:53

    As an alternative to the answer found here, you can use an ExpandoObject to quickly and pretty easily render a single row as JSON, as such:

    var expando = new System.Dynamic.ExpandoObject() as IDictionary;
    foreach (DataColumn col in myRow.Table.Columns)
    {
        eo[col.ColumnName] = myRow[col];
    }
    var json = JsonConvert.SerializeObject(expando);
    

提交回复
热议问题