使用 json.net/RestSharp 在 C# 中使用元数据反序列化 JSON

总是Thresh

我正在尝试创建一个数据集,其中包含我从 API 接收的 JSON 文件中的信息。

我不断收到 JsonSerializationException 作为“读取时意外的 JSON 令牌...”发生。意外的标记是一些我似乎无法摆脱的元数据(“分页”等)。有没有办法忽略元数据或什至不首先接收它?

json文件:

 {
  "paging": {
    "size": 8
  },
  "data": {
    "devices": [
      {
        "hostName": "x",
        "networkAddress": "x",
        "bestState": "Down",
        "worstState": "Down",
        "name": "x",
        "id": "1301"
      },
      {
        "hostName": "x",
        "networkAddress": "x",
        "bestState": "Down",
        "worstState": "Down",
        "name": "x",
        "id": "1299"
      },
      {
        "hostName": "x",
        "networkAddress": "x",
        "bestState": "Down",
        "worstState": "Down",
        "name": "x",
        "id": "1296"
      },
      {
        "hostName": "x",
        "networkAddress": "x",
        "bestState": "Down",
        "worstState": "Down",
        "name": "x",
        "id": "1291"
      },
      {
        "hostName": "x",
        "networkAddress": "x",
        "bestState": "Down",
        "worstState": "Down",
        "name": "x",
        "id": "1261"
      },
      {
        "hostName": "x",
        "networkAddress": "x",
        "bestState": "Down",
        "worstState": "Down",
        "name": "x",
        "id": "1249"
      },
      {
        "hostName": "x",
        "networkAddress": "x",
        "bestState": "Up",
        "worstState": "Down",
        "name": "x",
        "id": "157"
      },
      {
        "hostName": "x",
        "networkAddress": "x",
        "bestState": "Up",
        "worstState": "Down",
        "name": "x",
        "id": "205"
      }
    ]
  }
}

我的代码

             //Following block (2) taken from Postman, looks for device down group using bearer token retreived from block (1)
            var client2 = new RestClient("x");
            client2.Timeout = -1;
            var request2 = new RestRequest(Method.GET);
            request2.AddHeader("Authorization", "bearer " + bearer.access_token);
            IRestResponse response2 = client2.Execute(request2);
            textBox1.Text = response2.Content;
            //end of block (2)

            //Deserialize dataset from downDevices
            DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(response2.Content);
            DataTable dataTable = dataSet.Tables["devices"];
            foreach (DataRow row in dataTable.Rows)
            {
                textBox1.AppendText(row["name"] + " - " + row["networkAddress"]);

            }

我发现但迄今为止无法尝试的唯一潜在解决方案是在 Newtonsoft 文档中:https ://www.newtonsoft.com/json/help/html/DeserializeMetadataPropertyHandling.htm但是,我不确定是否有将此对象与数据集结合使用的方法。

            object o = JsonConvert.DeserializeObject(response2.Content, new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All,

                MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead //maybe use ignore?
            });
            //end of test Object

如果这是一个相当基本/愚蠢的问题或似乎重复,我深表歉意,我已经解决了尽可能多的类似问题并尝试了一些潜在的解决方案但无济于事。如果我的任何术语很奇怪/错误,我进一步道歉,我对 C# 和 API 整体来说是全新的。

提前致谢!

链表T

问题是您如何尝试反序列化。DataTable如果您只需要反序列化Devices对象,反序列化就会起作用但是随着Paging(不是有效的数据集)和Dataobjs 的添加,您需要编写一个自定义类来反序列化这个 obj 并将其指向DataTable. 像这样的东西:

public static void Deserialize()
    {
      string response2 = "{ \"paging\": { \"size\": 8 }, \"data\": { \"devices\": [ { \"hostName\": \"x\", \"networkAddress\": \"x\", \"bestState\": \"Down\", \"worstState\": \"Down\", \"name\": \"x\", \"id\": \"1301\" }] } } ";
      var dataSet = JsonConvert.DeserializeObject<Target>(response2);
      Console.WriteLine(dataSet);

      DataTable dataTable = dataSet.data.devices;
      Console.WriteLine(dataTable);

      foreach(DataRow row in dataTable.Rows)
      {
        Console.WriteLine(row["hostname"]+" :name");
      }         
    }
}
class Target
{
    public object paging{get;set;}
    public Device data{get;set;}
}

class Device
{
    public DataTable devices {get;set;}
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章