我想将json值转换为datatable,但是当我转换为datatable时,它缺少要在datatable中转换的id值。
Json
{
"territories": {
"a10b4b1e-8400-4613-8c90-5d282de6a8a6": {
"id": "a10b4b1e-8400-4613-8c90-5d282de6a8a6",
"name": "Connecticut",
"center": [
41603071,
-73087651
],
"timeZoneCode": "America/New_York",
"languageCode": null
},
"dc3d9d82-611e-41ba-a6f6-2c33ac5eebd1": {
"id": "dc3d9d82-611e-41ba-a6f6-2c33ac5eebd1",
"name": "Orlando",
"center": [
28538321,
-81379242
],
"timeZoneCode": "America/New_York",
"languageCode": null
}
}
}
将json转换为datatable的代码
private void ConvertJsonToDataTable( string jasonstring)
{
try
{
string jsonString = jasonstring;
if (!String.IsNullOrWhiteSpace(jsonString))
{
dynamic dynObj = JsonConvert.DeserializeObject(jsonString);
var sOffers = dynObj.territories;
// var sCoupons = dynObj.coupons;
DataTable dtPromotions = new DataTable();
dtPromotions.Columns.Add("id", typeof(string));
dtPromotions.Columns.Add("name", typeof(string));
dtPromotions.Columns.Add("timeZoneCode", typeof(string));
dtPromotions.Columns.Add("languageCode", typeof(string));
foreach (var cou in sOffers)
{
string cou1 = Convert.ToString(cou);
string[] RowData = Regex.Split(cou1.Replace("{", "").Replace("}", ""), ",");
DataRow nr = dtPromotions.NewRow();
foreach (string rowData in RowData)
{
try
{
int idx = rowData.IndexOf(":");
string RowColumns = rowData.Substring(0, idx - 1).Replace("\"", "").Trim();
string RowDataString = rowData.Substring(idx + 1).Replace("\"", "");
nr[RowColumns] = RowDataString;
}
catch (Exception ex)
{
continue;
}
}
dtPromotions.Rows.Add(nr);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ERROR",MessageBoxButtons.OK, MessageBoxIcon.Information);
}
答案 0 :(得分:0)
你应该只更换一行:
string cou1 = Convert.ToString(cou);
用这个:
string cou1 = Convert.ToString(cou.Value);
答案 1 :(得分:0)
我会这样做 -
var json = "{'territories':{'a10b4b1e-8400-4613-8c90-5d282de6a8a6':{'id':'a10b4b1e-8400-4613-8c90-5d282de6a8a6','name':'Connecticut','center':[41603071,-73087651],'timeZoneCode':'America/New_York','languageCode':null},'dc3d9d82-611e-41ba-a6f6-2c33ac5eebd1':{'id':'dc3d9d82-611e-41ba-a6f6-2c33ac5eebd1','name':'Orlando','center':[28538321,-81379242],'timeZoneCode':'America/New_York','languageCode':null}}}";
var obj = JsonConvert.DeserializeObject<RootObject>(json);
var dt = new DataTable();
dt.Columns.Add("id", typeof (string));
dt.Columns.Add("name", typeof (string));
dt.Columns.Add("timeZoneCode", typeof (string));
dt.Columns.Add("languageCode", typeof (string));
dt.Rows.Add(obj.territories.A10b4b1e840046138c905d282de6a8a6.id,
obj.territories.A10b4b1e840046138c905d282de6a8a6.name,
obj.territories.A10b4b1e840046138c905d282de6a8a6.timeZoneCode,
obj.territories.A10b4b1e840046138c905d282de6a8a6.languageCode);
dt.Rows.Add(obj.territories.Dc3d9d82611e41baA6f62c33ac5eebd1.id,
obj.territories.Dc3d9d82611e41baA6f62c33ac5eebd1.name,
obj.territories.Dc3d9d82611e41baA6f62c33ac5eebd1.timeZoneCode,
obj.territories.Dc3d9d82611e41baA6f62c33ac5eebd1.languageCode);
其中 -
public class A10b4b1e840046138c905d282de6a8a6
{
public string id { get; set; }
public string name { get; set; }
public List<int> center { get; set; }
public string timeZoneCode { get; set; }
public object languageCode { get; set; }
}
public class Dc3d9d82611e41baA6f62c33ac5eebd1
{
public string id { get; set; }
public string name { get; set; }
public List<int> center { get; set; }
public string timeZoneCode { get; set; }
public object languageCode { get; set; }
}
public class Territories
{
[JsonProperty("a10b4b1e-8400-4613-8c90-5d282de6a8a6")]
public A10b4b1e840046138c905d282de6a8a6 A10b4b1e840046138c905d282de6a8a6{get; set;}
[JsonProperty("dc3d9d82-611e-41ba-a6f6-2c33ac5eebd1")]
public Dc3d9d82611e41baA6f62c33ac5eebd1 Dc3d9d82611e41baA6f62c33ac5eebd1{get; set;}
}
public class RootObject
{
public Territories territories{get; set;}
}