如何从网上获取Json信息

时间:2016-12-21 09:33:16

标签: c# json

如何将网络资源中的信息转换为Json?在网上注册为json ..

根据我想在表格信息中显示的值

我为使用翻译的语言道歉

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; }
          }

1 个答案:

答案 0 :(得分:0)

将您的课程改为:

package1.MyClass1.MyInnerClass -> myClass1.MyInnerClass

反序列化:

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; }
}

访问以下值:

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

为了简洁起见,我错过了很多其他属性,整个课程会更复杂一些。

JSON必须完整地反序列化,因为它代表一个完整的对象,你不能一次挑选一行来获得单独的属性。