我正在尝试通过检查有效ID的上次激活时间来构建有效ID的数组。我就在终点,当我找到一个活动的设备时,我得到了一个空异常错误。“ thestream”变量中包含JSON并包含以下内容:
{“ domain”:“ d50fb707f9317”,“ stuff”:“ comm”,“ thing”:“ 000780000001”,“ m2m_raw”:“ {\” command \“:100,\” current-flow \“:25,\ “ cap-max \”:1200,\“ cap-remain \”:339}“,” m2m_nestedkey“:[”系统特性“,”诊断信息“,”诊断信息2“,”系统设置“,”阀门设置“ ],“ clock”:0,“ attributes”:{“ current-flow:clock”:“ 1401480786291382”,“ weight”:“ 150”,“ cap-max”:“ 1200”,“ myname”:“ valve12” ,“ current-flow”:“ 25”,“ command”:“ 100”,“ hello:clock”:“ 1401201915442520”,“ myname:clock”:“ 1401276113497347”,“ cap-remain:clock”:“ 1401480786291382”,“ hello”:“有”,“ cap-max:clock”:“ 1401480786291382”,“ command:clock”:“ 1401480786291382”,“ cap-remain”:“ 339 “,” weight:clock“:” 1401275692557475“}}
我的模型中有以下声明:
public class ListofDevices
{
public long[] Devices { get; set; }
}
像这样在控制器中声明:
var TheList = new ListofDevices();
声明activeCount并将其设置为零:
byte activeCount = 0; //(the highest number in here is 36)
下面的所有代码都在控制器中。
JObject CheckIt = JObject.Parse(thestream); // parse the Json as best as we can, given the json we are getting back
string ClockNumString = (string)(((Newtonsoft.Json.Linq.JValue)((((((Newtonsoft.Json.Linq.JContainer)(CheckIt)).Last).First).First).First)).Value);
long ClockNum = Convert.ToInt64(ClockNumString); // convert the string into a number
ClockNum = ClockNum / 1000000; // convert to seconds
TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1); // get the current epoch time
long secondsSinceEpoch = (long)t.TotalSeconds; // convert to seconds for comparison
if ((secondsSinceEpoch - ClockNum) < 60000) {
TheList.Devices[activeCount] = Convert.ToInt64(uniqueitems[i]); // ERROR IS HERE
activeCount++;
}
我疯狂的Newtonsoft.Json.Linq命令是由于无法以其他任何方式处理返回的JSON。我输入到ClockNumString中的数据是正确的。但是,当我尝试将项目存储到活动数组中并传递if语句时,出现NullRefenceException错误。它似乎是一个类型错误(?),但我一直在寻求stackoverflow的帮助,并且整天都在工作,但没有运气。任何帮助,不胜感激。
Devices
从TheList
作为没有被初始化,和/或uniqueitems
不被初始化,使用new
初始化对象或阵列,即:
TheList.Devices = new long[N];
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句