0

how can I access in this JSON (http://www.pegelonline.wsv.de/webservices/rest-api/v2/stations.json?includeTimeseries=true&includeCurrentMeasurement=true) the nested array like timeseries.shortname? I tried like this but it doesn't work.

string url = "http://www.pegelonline.wsv.de/webservices/rest-api/v2/stations.json?includeTimeseries=true&includeCurrentMeasurement=true";

HttpWebRequest request = HttpWebRequest.CreateHttp(url);
WebResponse response = await request.GetResponseAsync();

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

    foreach (var item in info)
    {
        myModel.Add(new ItemModel()
        {
            uuid = item.uuid,
            number = item.number,
            city_longname = item.longname,
            timeseries = item.timeseries.shortname
        });
    }
}

The 3 items works, but the last (timeseries) gives the following error: Cannot perform runtime binding on a null reference

3
  • What is the actual JSON response? I would suspect it is missing or is being accessed incorrectly. Make sure to include the relevant data in questions (and don't expect people to follow links :). Commented Sep 22, 2013 at 18:40
  • the VS debugger only gives the "Cannot perform runtime binding on a null reference" response Commented Sep 22, 2013 at 18:46
  • 3
    timeseries is an array, try item.timeseries[0].shortname Commented Sep 22, 2013 at 18:54

1 Answer 1

0

The dynamic properties give you JToken objects. Using the Value property on those gives you the string representation. In order to get it type-safe you need to parse/convert. Since you did not provide your ItemModel class details I cannot help you here.

myModel.Add(new ItemModel()
{
  uuid = item.uuid.Value,
  number = item.number.Value,
  city_longname = item.longname.Value
});

The timeseries property is a JArray object. You cannot get to the shortname property directly. You have to choose an index first (item.timeseries[5], for instance, give you the JObject instance you are after). The details regarding getting the actual values in a type-safe manner from above apply here as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.