使用C#MVC访问Cosmos Db JSON数据
我试图使用c#MVC Web应用程序显示Cosmos db JSON子项。 但我得到了一个错误: 无法从System.DateTime强制转换或转换为WebApplication1.Models.Entry JSON:使用C#MVC访问Cosmos Db JSON数据,c#,json,nested,azure-cosmosdb,subitem,C#,Json,Nested,Azure Cosmosdb,Subitem,我试图使用c#MVC Web应用程序显示Cosmos db JSON子项。 但我得到了一个错误: 无法从System.DateTime强制转换或转换为WebApplication1.Models.Entry JSON: "DataTypeId": 1, "IPaddress": "192.168.2.177", "id": "d1b81653-b7a5-4d79-85ea-79c01f43f747", "PhoneNo": "0417518324", "LastContact": {
"DataTypeId": 1,
"IPaddress": "192.168.2.177",
"id": "d1b81653-b7a5-4d79-85ea-79c01f43f747",
"PhoneNo": "0417518324",
"LastContact": {
"EnvLog": "2019-09-07T19:41:08",
"Ping": "2020-01-09T12:10:09",
"SpdLog": "2019-09-07T19:41:08"
},
"SpeedLog": true,
"DeviceModelId": 2,
"DisconnectEvents": 9,
public class DeviceMap
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "IPaddress")]
public string IPaddress { get; set; }
[JsonProperty(PropertyName = "SpeedLog")]
public bool SpeedLog { get; set; }
[JsonProperty(PropertyName = "LastContact")]
public Entry LastContact { get; set; }
}
public class Entry
{
[JsonProperty(PropertyName = "EnvLog")]
public DateTime EnvLogtime { get; set; }
[JsonProperty(PropertyName = "Ping")]
public DateTime Ping { get; set; }
[JsonProperty(PropertyName = "SpdLog")]
public DateTime SpdLog { get; set; }
}
}
public class DeviceMapController : Controller
{
[ActionName("Index")]
public async Task<ActionResult> Map1Async()
{
string empty = "Unknown";
var items = await DocumentDBRepository<DeviceMap>.GetMap1Async(d => d.Id != null, d => d.Lat != empty);
if (Convert.ToInt32(User.GroupId()) != 200)
{
items = items.Where(d => d.GroupId == Convert.ToInt32(User.GroupId()));
}
return View(items);
}
}
@foreach (var item in Model)
{
if ((@item.IPaddress == "192.168.2.14") || (@item.IPaddress == "192.168.2.13") || (@item.IPaddress == "192.168.2.15") || (@item.IPaddress == "192.168.2.16")) { }
else
{
//SideId string
string siteId = null;
string newsiteId = null;
if (item.IPaddress != "0")
{
int index = item.IPaddress.IndexOf('.') + 1;
int space = item.IPaddress.IndexOf('.', index + 1);
int space2 = item.IPaddress.IndexOf('.', space + 1);
int end = item.IPaddress.Length;
if (index > 0) { siteId = item.IPaddress.Substring(space + 1, end - space - 1); newsiteId = siteId.Replace('.', '-'); }
}
// string Cutoff =Html.DisplayFor(modelItem => item.LastContact.Ping).ToString();
// DateTime day = DateTime.Parse(Cutoff.Value,"dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture);
DateTime myday =Html.DisplayFor(modelItem => item.LastContact.Ping);
DateTime now = DateTime.Now;
DateTime yesterday = now.AddDays(-7);
DateTime d;
// DateTime.TryParseExact(myday,@"dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal,out d);
// if (Html.DisplayFor(modelItem => item.LastContact.Ping) != null) { d = DateTime.Parse(myday, "dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture); } else { myday = DateTime.ParseExact("08/04/2000 14:00:02", "dd/MM/yyyy HH: mm:ss", CultureInfo.CurrentCulture); }
string a = @item.Name;
<tr>
<td>
@if (a.Length > 50) { a = a.Substring(0, 50); }
@if (item.GroupId != 82)
{
if (myday <= DateTime.Now.AddDays(-7))
{<h4>@myday</h4>
<img src="/Content/Images/trafficlight-red2.png" width="20" height="20"><a href="@Url.Action("DevicePage", "Device", new { item.DeviceId })"><h6>@a..</h6></a>
}
else
{<img src="/Content/Images/trafficlight-green1.png" width="20" height="20"><a href="@Url.Action("DevicePage", "Device", new { item.DeviceId })"><h6>@a..</h6></a>
}
}
else
{
if (myday <= DateTime.Now.AddDays(-7))
{
<img src="/Content/Images/trafficlight-red2.png" width="20" height="20"><a href="@Url.Action("Form", "TARP", new { item.DeviceId })"><h6>@a..</h6></a>
}
else
{<img src="/Content/Images/trafficlight-green1.png" width="20" height="20"><a href="@Url.Action("Form", "TARP", new { item.DeviceId })"><h6>@a..</h6></a>
}
}
@if (item.IPaddress != "0")
{<p>Site Id: @newsiteId;</p>}
@if (item.LastPing != null)
{
<p>
Last Contact: @Html.DisplayFor(modelItem => item.LastContact.Ping), @Html.DisplayFor(modelItem => item.BatVoltage)V
</p>}
型号:
"DataTypeId": 1,
"IPaddress": "192.168.2.177",
"id": "d1b81653-b7a5-4d79-85ea-79c01f43f747",
"PhoneNo": "0417518324",
"LastContact": {
"EnvLog": "2019-09-07T19:41:08",
"Ping": "2020-01-09T12:10:09",
"SpdLog": "2019-09-07T19:41:08"
},
"SpeedLog": true,
"DeviceModelId": 2,
"DisconnectEvents": 9,
public class DeviceMap
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "IPaddress")]
public string IPaddress { get; set; }
[JsonProperty(PropertyName = "SpeedLog")]
public bool SpeedLog { get; set; }
[JsonProperty(PropertyName = "LastContact")]
public Entry LastContact { get; set; }
}
public class Entry
{
[JsonProperty(PropertyName = "EnvLog")]
public DateTime EnvLogtime { get; set; }
[JsonProperty(PropertyName = "Ping")]
public DateTime Ping { get; set; }
[JsonProperty(PropertyName = "SpdLog")]
public DateTime SpdLog { get; set; }
}
}
public class DeviceMapController : Controller
{
[ActionName("Index")]
public async Task<ActionResult> Map1Async()
{
string empty = "Unknown";
var items = await DocumentDBRepository<DeviceMap>.GetMap1Async(d => d.Id != null, d => d.Lat != empty);
if (Convert.ToInt32(User.GroupId()) != 200)
{
items = items.Where(d => d.GroupId == Convert.ToInt32(User.GroupId()));
}
return View(items);
}
}
@foreach (var item in Model)
{
if ((@item.IPaddress == "192.168.2.14") || (@item.IPaddress == "192.168.2.13") || (@item.IPaddress == "192.168.2.15") || (@item.IPaddress == "192.168.2.16")) { }
else
{
//SideId string
string siteId = null;
string newsiteId = null;
if (item.IPaddress != "0")
{
int index = item.IPaddress.IndexOf('.') + 1;
int space = item.IPaddress.IndexOf('.', index + 1);
int space2 = item.IPaddress.IndexOf('.', space + 1);
int end = item.IPaddress.Length;
if (index > 0) { siteId = item.IPaddress.Substring(space + 1, end - space - 1); newsiteId = siteId.Replace('.', '-'); }
}
// string Cutoff =Html.DisplayFor(modelItem => item.LastContact.Ping).ToString();
// DateTime day = DateTime.Parse(Cutoff.Value,"dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture);
DateTime myday =Html.DisplayFor(modelItem => item.LastContact.Ping);
DateTime now = DateTime.Now;
DateTime yesterday = now.AddDays(-7);
DateTime d;
// DateTime.TryParseExact(myday,@"dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal,out d);
// if (Html.DisplayFor(modelItem => item.LastContact.Ping) != null) { d = DateTime.Parse(myday, "dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture); } else { myday = DateTime.ParseExact("08/04/2000 14:00:02", "dd/MM/yyyy HH: mm:ss", CultureInfo.CurrentCulture); }
string a = @item.Name;
<tr>
<td>
@if (a.Length > 50) { a = a.Substring(0, 50); }
@if (item.GroupId != 82)
{
if (myday <= DateTime.Now.AddDays(-7))
{<h4>@myday</h4>
<img src="/Content/Images/trafficlight-red2.png" width="20" height="20"><a href="@Url.Action("DevicePage", "Device", new { item.DeviceId })"><h6>@a..</h6></a>
}
else
{<img src="/Content/Images/trafficlight-green1.png" width="20" height="20"><a href="@Url.Action("DevicePage", "Device", new { item.DeviceId })"><h6>@a..</h6></a>
}
}
else
{
if (myday <= DateTime.Now.AddDays(-7))
{
<img src="/Content/Images/trafficlight-red2.png" width="20" height="20"><a href="@Url.Action("Form", "TARP", new { item.DeviceId })"><h6>@a..</h6></a>
}
else
{<img src="/Content/Images/trafficlight-green1.png" width="20" height="20"><a href="@Url.Action("Form", "TARP", new { item.DeviceId })"><h6>@a..</h6></a>
}
}
@if (item.IPaddress != "0")
{<p>Site Id: @newsiteId;</p>}
@if (item.LastPing != null)
{
<p>
Last Contact: @Html.DisplayFor(modelItem => item.LastContact.Ping), @Html.DisplayFor(modelItem => item.BatVoltage)V
</p>}
控制器:
"DataTypeId": 1,
"IPaddress": "192.168.2.177",
"id": "d1b81653-b7a5-4d79-85ea-79c01f43f747",
"PhoneNo": "0417518324",
"LastContact": {
"EnvLog": "2019-09-07T19:41:08",
"Ping": "2020-01-09T12:10:09",
"SpdLog": "2019-09-07T19:41:08"
},
"SpeedLog": true,
"DeviceModelId": 2,
"DisconnectEvents": 9,
public class DeviceMap
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "IPaddress")]
public string IPaddress { get; set; }
[JsonProperty(PropertyName = "SpeedLog")]
public bool SpeedLog { get; set; }
[JsonProperty(PropertyName = "LastContact")]
public Entry LastContact { get; set; }
}
public class Entry
{
[JsonProperty(PropertyName = "EnvLog")]
public DateTime EnvLogtime { get; set; }
[JsonProperty(PropertyName = "Ping")]
public DateTime Ping { get; set; }
[JsonProperty(PropertyName = "SpdLog")]
public DateTime SpdLog { get; set; }
}
}
public class DeviceMapController : Controller
{
[ActionName("Index")]
public async Task<ActionResult> Map1Async()
{
string empty = "Unknown";
var items = await DocumentDBRepository<DeviceMap>.GetMap1Async(d => d.Id != null, d => d.Lat != empty);
if (Convert.ToInt32(User.GroupId()) != 200)
{
items = items.Where(d => d.GroupId == Convert.ToInt32(User.GroupId()));
}
return View(items);
}
}
@foreach (var item in Model)
{
if ((@item.IPaddress == "192.168.2.14") || (@item.IPaddress == "192.168.2.13") || (@item.IPaddress == "192.168.2.15") || (@item.IPaddress == "192.168.2.16")) { }
else
{
//SideId string
string siteId = null;
string newsiteId = null;
if (item.IPaddress != "0")
{
int index = item.IPaddress.IndexOf('.') + 1;
int space = item.IPaddress.IndexOf('.', index + 1);
int space2 = item.IPaddress.IndexOf('.', space + 1);
int end = item.IPaddress.Length;
if (index > 0) { siteId = item.IPaddress.Substring(space + 1, end - space - 1); newsiteId = siteId.Replace('.', '-'); }
}
// string Cutoff =Html.DisplayFor(modelItem => item.LastContact.Ping).ToString();
// DateTime day = DateTime.Parse(Cutoff.Value,"dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture);
DateTime myday =Html.DisplayFor(modelItem => item.LastContact.Ping);
DateTime now = DateTime.Now;
DateTime yesterday = now.AddDays(-7);
DateTime d;
// DateTime.TryParseExact(myday,@"dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal,out d);
// if (Html.DisplayFor(modelItem => item.LastContact.Ping) != null) { d = DateTime.Parse(myday, "dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture); } else { myday = DateTime.ParseExact("08/04/2000 14:00:02", "dd/MM/yyyy HH: mm:ss", CultureInfo.CurrentCulture); }
string a = @item.Name;
<tr>
<td>
@if (a.Length > 50) { a = a.Substring(0, 50); }
@if (item.GroupId != 82)
{
if (myday <= DateTime.Now.AddDays(-7))
{<h4>@myday</h4>
<img src="/Content/Images/trafficlight-red2.png" width="20" height="20"><a href="@Url.Action("DevicePage", "Device", new { item.DeviceId })"><h6>@a..</h6></a>
}
else
{<img src="/Content/Images/trafficlight-green1.png" width="20" height="20"><a href="@Url.Action("DevicePage", "Device", new { item.DeviceId })"><h6>@a..</h6></a>
}
}
else
{
if (myday <= DateTime.Now.AddDays(-7))
{
<img src="/Content/Images/trafficlight-red2.png" width="20" height="20"><a href="@Url.Action("Form", "TARP", new { item.DeviceId })"><h6>@a..</h6></a>
}
else
{<img src="/Content/Images/trafficlight-green1.png" width="20" height="20"><a href="@Url.Action("Form", "TARP", new { item.DeviceId })"><h6>@a..</h6></a>
}
}
@if (item.IPaddress != "0")
{<p>Site Id: @newsiteId;</p>}
@if (item.LastPing != null)
{
<p>
Last Contact: @Html.DisplayFor(modelItem => item.LastContact.Ping), @Html.DisplayFor(modelItem => item.BatVoltage)V
</p>}
公共类设备主控制器:控制器
{
[行动名称(“索引”)]
公共异步任务
}
其他的
{
}
}
其他的
{
if(myday item.LastContact.Ping),@Html.DisplayFor(modelItem=>item.BatVoltage)V
}
我想显示日期和时间。请帮忙。
另外,如果您有关于如何调用不同样式的JSON数据的资源,请提供链接以供参考。
多谢各位
public class DeviceMap
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; } // Suggestion: Change this to 'Guid' instead of string
[JsonProperty(PropertyName = "IPaddress")]
public string IPaddress { get; set; }
[JsonProperty(PropertyName = "SpeedLog")]
public bool SpeedLog { get; set; }
[JsonProperty(PropertyName = "LastContact")]
public Entry LastContact { get; set; }
}
public class Entry
{
[JsonProperty(PropertyName = "EnvLog")]
public DateTime EnvLogtime { get; set; }
[JsonProperty(PropertyName = "Ping")]
public DateTime Ping { get; set; }
[JsonProperty(PropertyName = "SpdLog")]
public DateTime SpdLog { get; set; }
}
如果在条目
中选择日期时间
作为每个变量的类型,您的上一个联系人将正确反序列化
测试
我使用了您的json并将其转换为DeviceMap对象来测试它,并确保上述功能正常工作
{
"DataTypeId": 1,
"IPaddress": "192.168.2.177",
"id": "d1b81653-b7a5-4d79-85ea-79c01f43f747",
"PhoneNo": "0417518324",
"LastContact": {
"EnvLog": "2019-09-07T19:41:08",
"Ping": "2020-01-09T12:10:09",
"SpdLog": "2019-09-07T19:41:08"
},
"SpeedLog": true,
"DeviceModelId": 2,
"DisconnectEvents": 9
}
并在我的主要代码中使用以下代码对其进行反序列化,以确保其正确工作
DeviceMap map = JsonConvert.DeserializeObject<DeviceMap>(json);
DeviceMap=JsonConvert.DeserializeObject(json);
最好在类中定义类型,这样在进行反序列化时,就不必将其解析为应该的类型。反序列化程序应该为您解决这个问题,并将字符串转换为类的类型(如果它是可转换的),例如,自动将字符串转换为JSON中的
Guid
或DateTime
,LastContact
是单个Entry
对象。但是在您的型号中,LastContact
属于Dictionary
类型。应该是什么?LastContact是单条目对象。但不知道如何调用它将条目
类中的对象更改为日期时间
并将字典
更改为仅条目
。能否请您进一步解释或提供信息链接@jawad在添加反序列化代码后,我收到错误“CS0103 C”#名称“json”在当前上下文中不存在我还添加了Newtonsoft.Json库@Jawadjson是一个字符串,它保存json数据的内容。这是我的测试变量。@Ana,你能解决这个问题吗?没有,还在挣扎@贾瓦德,你能解释一下吗more@Ana你更新过这里提到的课程吗?如何反序列化json