jeet jeet - 6 months ago 19
JSON Question

Converting Multiple JSON To Object JSON.NET

So I have a task which needs me to deserialize JSON to C# Objects and process them later. Please bear with me, the JSON and Class files are large.

The sample JSON output that I am parsing looks like this:

{
"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
}
]
}


Note that the
topology
node (I am not sure of the jargon) has more than one value stored. The number of values here is a variable, that is it increases with time.

The classes I generated to parse this JSON is this (created using 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; }
}


My question is I saw the Newtonsoft JSON.net example to deserialize, which shows viz:

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


My question is: How do I do it for a multiple object (not sure of jargon, again) JSON, like mines?

P.S. I am accessing the json remotely like:

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


Any pointers, samples etc will be helpful.
Thanks.

Answer

Just try :

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

You have list of topologies in the gizmo class not list of Gizmos. From there you can get List of topologies.

If you have list of gizmos you can get them like this:

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

Here is how you can get all the topologies:

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