简体   繁体   English

反序列化复杂json时获取NULL值

[英]Getting NULL values while deserializing complex json

My project has a 3rd party web API that returns a json string in the following format (including the starting and ending curly braces): 我的项目有一个第三方Web API,该API返回以下格式的json字符串(包括大括号的开头和结尾):

{ 
   "866968030210604":{
      "dt_server":"2019-02-07 12:21:27",
      "dt_tracker":"2019-02-07 12:21:27",
      "lat":"28.844968",
      "lng":"76.858502",
      "altitude":"0",
      "angle":"154",
      "speed":"9",
      "params":{
         "pump":"0",
         "track":"1",
         "bats":"1",
         "acc":"0",
         "batl":"4"
      },
      "loc_valid":"1"
   },
   "866968030221205":{
      "dt_server":"2019-02-07 12:20:24",
      "dt_tracker":"2019-02-07 12:19:41",
      "lat":"28.845904",
      "lng":"77.096063",
      "altitude":"0",
      "angle":"0",
      "speed":"0",
      "params":{
         "pump":"0",
         "track":"1",
         "bats":"1",
         "acc":"0",
         "batl":"4"
      },
      "loc_valid":"1"
   },
   "866968030212030":{
      "dt_server":"0000-00-00 00:00:00",
      "dt_tracker":"0000-00-00 00:00:00",
      "lat":"0",
      "lng":"0",
      "altitude":"0",
      "angle":"0",
      "speed":"0",
      "params":null,
      "loc_valid":"0"
   }
}

I want to deserialize it into ac# class object for further processing. 我想将其反序列化为ac#类对象以进行进一步处理。 I made the following class structure for the same: 我为相同的语言制作了以下类结构:

class Params
{
    public string pump { get; set; }
    public string track { get; set; }
    public string bats { get; set; }
    public string acc { get; set; }
    public string batl { get; set; }
}

class GPSData
{
    public string dt_server { get; set; }
    public string dt_tracker { get; set; }
    public string lat { get; set; }
    public string lng { get; set; }
    public string altitude { get; set; }
    public string angle { get; set; }
    public string speed { get; set; }
    public Params ObjParams { get; set; }
    public string loc_valid { get; set; }
}

and I am trying the following code to deserialize: 我正在尝试以下代码反序列化:

JavaScriptSerializer jSerObj = new JavaScriptSerializer();

List<GPSData> lstGPSData = (List<GPSData>)jSerObj.Deserialize(json, typeof(List<GPSData>));

But every time it is showing NULL values assigned to each property of the class after the Deserialize() method is called. 但是每次调用Deserialize()方法后,每次显示分配给该类的每个属性的NULL值。 Please help me on this. 请帮我。

Your json is not in list format so deserializing to List<> isn't work 您的json格式不是列表格式,因此反序列化为List<>无效

So you need to deserialize it into Dictionary<string, GPSData> like 因此,您需要将其反序列化为Dictionary<string, GPSData>

JavaScriptSerializer jSerObj = new JavaScriptSerializer();

Dictionary<string, GPSData> lstGPSData = (Dictionary<string, GPSData>)jSerObj.Deserialize(json, typeof(Dictionary<string, GPSData>));

Usage: 用法:

foreach (var item in lstGPSData)
{
    string key = item.Key;
    GPSData gPSData = item.Value;
}

Also, you can list all your GPSData from above dictionary like, 另外,您可以列出以上字典中的所有GPSData ,例如,

List<GPSData> gPSDatas = lstGPSData.Values.ToList();

Output: (From Debugger) 输出:(来自调试器)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM