将多个JSON转换为对象JSON.NET

时间:2016-06-05 14:58:10

标签: c# json json.net

所以我有一个任务需要我将JSON反序列化为C#对象并稍后处理它们。请耐心等待,JSON和Class文件很大。

我正在解析的示例JSON输出如下所示:

{
  "systemTime": 1465126640,
  "timeSinceStartup": 174758912,
  "neighbors": [
    {
      "ipAddress": "10.0.0.2",
      "symmetric": true,
      "multiPointRelay": true,
      "multiPointRelaySelector": false,
      "willingness": 3,
      "twoHopNeighborCount": 1
    }
  ],
  "links": [
    {
      "localIP": "10.0.0.1",
      "remoteIP": "10.0.0.2",
      "validityTime": 38810,
      "linkQuality": 1.000,
      "neighborLinkQuality": 1.000,
      "linkCost": 1024
    }
  ],
  "routes": [
    {
      "destination": "10.0.0.2",
      "genmask": 32,
      "gateway": "10.0.0.2",
      "metric": 1,
      "rtpMetricCost": 1024,
      "networkInterface": "mesh0"
    },
    {
      "destination": "10.0.0.3",
      "genmask": 32,
      "gateway": "10.0.0.2",
      "metric": 2,
      "rtpMetricCost": 2531,
      "networkInterface": "mesh0"
    },
    {
      "destination": "10.0.0.4",
      "genmask": 32,
      "gateway": "10.0.0.2",
      "metric": 3,
      "rtpMetricCost": 3555,
      "networkInterface": "mesh0"
    },
    {
      "destination": "10.0.0.5",
      "genmask": 32,
      "gateway": "10.0.0.2",
      "metric": 3,
      "rtpMetricCost": 4732,
      "networkInterface": "mesh0"
    },
    {
      "destination": "10.0.0.6",
      "genmask": 32,
      "gateway": "10.0.0.2",
      "metric": 4,
      "rtpMetricCost": 6151,
      "networkInterface": "mesh0"
    }
  ],
  "topology": [
    {
      "destinationIP": "10.0.0.2",
      "lastHopIP": "10.0.0.1",
      "linkQuality": 1.000,
      "neighborLinkQuality": 1.000,
      "tcEdgeCost": 1024,
      "validityTime": 0
    },
    {
      "destinationIP": "10.0.0.1",
      "lastHopIP": "10.0.0.2",
      "linkQuality": 1.000,
      "neighborLinkQuality": 1.000,
      "tcEdgeCost": 1024,
      "validityTime": 283361
    },
    {
      "destinationIP": "10.0.0.3",
      "lastHopIP": "10.0.0.2",
      "linkQuality": 0.940,
      "neighborLinkQuality": 0.721,
      "tcEdgeCost": 1507,
      "validityTime": 283361
    },
    {
      "destinationIP": "10.0.0.2",
      "lastHopIP": "10.0.0.3",
      "linkQuality": 0.721,
      "neighborLinkQuality": 0.940,
      "tcEdgeCost": 1507,
      "validityTime": 277026
    },
    {
      "destinationIP": "10.0.0.4",
      "lastHopIP": "10.0.0.3",
      "linkQuality": 1.000,
      "neighborLinkQuality": 1.000,
      "tcEdgeCost": 1024,
      "validityTime": 277026
    },
    {
      "destinationIP": "10.0.0.5",
      "lastHopIP": "10.0.0.3",
      "linkQuality": 0.662,
      "neighborLinkQuality": 0.701,
      "tcEdgeCost": 2201,
      "validityTime": 277026
    },
    {
      "destinationIP": "10.0.0.3",
      "lastHopIP": "10.0.0.4",
      "linkQuality": 1.000,
      "neighborLinkQuality": 0.940,
      "tcEdgeCost": 1088,
      "validityTime": 274952
    },
    {
      "destinationIP": "10.0.0.5",
      "lastHopIP": "10.0.0.4",
      "linkQuality": 0.940,
      "neighborLinkQuality": 0.529,
      "tcEdgeCost": 2055,
      "validityTime": 274952
    },
    {
      "destinationIP": "10.0.0.3",
      "lastHopIP": "10.0.0.5",
      "linkQuality": 0.701,
      "neighborLinkQuality": 0.662,
      "tcEdgeCost": 2201,
      "validityTime": 280956
    },
    {
      "destinationIP": "10.0.0.4",
      "lastHopIP": "10.0.0.5",
      "linkQuality": 0.607,
      "neighborLinkQuality": 0.940,
      "tcEdgeCost": 1789,
      "validityTime": 280956
    },
    {
      "destinationIP": "10.0.0.6",
      "lastHopIP": "10.0.0.5",
      "linkQuality": 1.000,
      "neighborLinkQuality": 0.721,
      "tcEdgeCost": 1419,
      "validityTime": 280956
    },
    {
      "destinationIP": "10.0.0.5",
      "lastHopIP": "10.0.0.6",
      "linkQuality": 0.831,
      "neighborLinkQuality": 0.940,
      "tcEdgeCost": 1308,
      "validityTime": 269860
    }
  ]
}

请注意,topology节点(我不确定行话)存储了多个值。这里的值的数量是一个变量,即它随着时间的推移而增加。

我为解析此JSON而生成的类是这个(使用http://jsonutils.com/创建):

 public class Neighbor
    {
        public string ipAddress { get; set; }
        public bool symmetric { get; set; }
        public bool multiPointRelay { get; set; }
        public bool multiPointRelaySelector { get; set; }
        public int willingness { get; set; }
        public int twoHopNeighborCount { get; set; }
    }

    public class Link
    {
        public string localIP { get; set; }
        public string remoteIP { get; set; }
        public int validityTime { get; set; }
        public double linkQuality { get; set; }
        public double neighborLinkQuality { get; set; }
        public int linkCost { get; set; }
    }

    public class Route
    {
        public string destination { get; set; }
        public int genmask { get; set; }
        public string gateway { get; set; }
        public int metric { get; set; }
        public int rtpMetricCost { get; set; }
        public string networkInterface { get; set; }
    }

    public class Topology
    {
        public string destinationIP { get; set; }
        public string lastHopIP { get; set; }
        public double linkQuality { get; set; }
        public double neighborLinkQuality { get; set; }
        public int tcEdgeCost { get; set; }
        public int validityTime { get; set; }
    }

    public class Gizmo
    {
        public int systemTime { get; set; }
        public int timeSinceStartup { get; set; }
        public IList<Neighbor> neighbors { get; set; }
        public IList<Link> links { get; set; }
        public IList<Route> routes { get; set; }
        public IList<Topology> topology { get; set; }
    }

我的问题是我看到要反序列化的Newtonsoft JSON.net示例,其中显示了:

string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";

Movie m = JsonConvert.DeserializeObject<Movie>(json);

string name = m.Name;
// Bad Boys

我的问题是:我如何为多个对象执行此操作(再次不确定行话)JSON,就像地雷一样?

P.S。我正在远程访问json:

WebClient client = new WebClient();
Stream stream = client.OpenRead(DLinks.jsonUrl1);
Debug.Assert(stream != null, "stream != null");
StreamReader reader = new StreamReader(stream);

任何指针,样品等都会有所帮助。 感谢。

1 个答案:

答案 0 :(得分:3)

试试吧:

Gizmo gizmo = JsonConvert.DeserializeObject<Gizmo>(jsonStr);

您有Gizmo类的拓扑列表,而不是Gizmos列表。 从那里你可以获得拓扑列表。

如果你有小发明清单,你可以这样得到它们:

List<Gizmo> gizmos JsonConvert.DeserializeObject<List<Gizmo>>(jsonStr);

以下是获取所有拓扑的方法:

Gizmo gizmo = JsonConvert.DeserializeObject<Gizmo>(jsonStr);
List<Topology> topologies = gizmo.topology.ToList();