0

How can I translate the information from the web source to Json? Registered as json on the web..

According to the values I want to show in the table information

I apologize for the language I use translation

MyCode :

var httpWebRequest = (HttpWebRequest)WebRequest.Create("link");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "GET";   


        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadLine();

            Airport kny = JsonConvert.DeserializeObject<Airport>(result);

            string fskodu = kny.fsCode; // fs kodu null whyy???

        }

    public class Airport
          {
              [JsonProperty("requestedCode")]
              public string requestedCode { get; set; }



              [JsonProperty("fsCode")]
              public string fsCode { get; set; }
          }
ynsbldk
  • 29
  • 7
  • Your code assumes the first line from the stream is valid JSON... Is it? Are there multiple lines in the data? Please show an example of your data – iCollect.it Ltd Dec 21 '16 at 09:37
  • Thanks Gone..Yes more than one data..{ "request": { "airport": { "requestedCode": "KYA", "fsCode": "KYA" }, "date": { "year": "2016", "month": "12", "day": "20", "interpreted": "2016-12-20" }, "hourOfDay": { "requested": "8", "interpreted": 8 }, "numHours": { "requested": "1", "interpreted": 1 }, "utc": { "requested": "false", "interpreted": false }, – ynsbldk Dec 21 '16 at 09:46
  • I understand now... will add an answer – iCollect.it Ltd Dec 21 '16 at 09:51
  • Possible duplicate of [reading HttpwebResponse json response, C#](http://stackoverflow.com/questions/5493949/reading-httpwebresponse-json-response-c-sharp) – Liam Dec 21 '16 at 10:02

1 Answers1

0

Change your class to look like this:

public class RootObject
{
    public Request request { get; set; }
}

public class Request
{
    public Airport airport { get; set; }
}

public class Airport
{
    public string requestedCode { get; set; }
    public string fsCode { get; set; }
}

Deserialize with:

var kny = JsonConvert.DeserializeObject<RootObject>(result);

Access values like this:

kny.request.airport.fsCode; // This is == "KYA"

I'm missing out a whole bunch of other properties for brevity, the full class would be a little more complex.

JSON has to be deserialized in its entirety as it represents a complete object, you can't cherry pick one line at a time to get individual properties.

Equalsk
  • 7,954
  • 2
  • 41
  • 67
  • An exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll but was not handled in user code Additional information: Input string '3.0' is not a valid integer. Path 'appendix.airports[0].utcOffsetHours', line 1, position 1109. – ynsbldk Dec 21 '16 at 12:26
  • Change the property 'utcoffsethours' in your airport class to a double instead of integer. – Equalsk Dec 21 '16 at 12:28