user2025830
user2025830

Reputation: 902

Parse JSON array with Json.net

how can I read the JSON from this site (http://www.pegelonline.wsv.de/webservices/rest-api/v2/stations.json?includeTimeseries=true&includeCurrentMeasurement=true) in C# with the Json.net library? In this JSon there is only a array. I tested it with this code, but it doesn't work.

using (Stream stream = response.GetResponseStream())
{
  JsonReader reader = new JsonTextReader(new StreamReader(stream));
  dynamic info = JObject.Load(reader);
}

If i debug this, then VS says that the item is not an object. I try it with JArray.Load(reader); but then I don't know how to access the items.

Upvotes: 0

Views: 4045

Answers (1)

meilke
meilke

Reputation: 3280

You are right, JArray.Load is working correctly. The JArray then has a simple indexer to get to the individual items:

using (Stream stream = response.GetResponseStream())
{
  var reader = new JsonTextReader(new StreamReader(stream));
  var jsonArray = JArray.Load(reader);
  var item20 = jsonArray[19];
  var item20ShortName = (string)item20["shortname"];
}

Upvotes: 1

Related Questions