【发布时间】:2013-01-30 15:04:09
【问题描述】:
我正在尝试反序列化来自 Google 地图地理编码的 JSON 响应。
这些是我根据响应编写的类。
public class GoogleGeoCodeResponse
{
public string status { get; set; }
public results[] results { get; set; }
}
public class results
{
public address_component[] address_components { get; set; }
public String formatted_address { get; set; }
public Geometry geometry { get; set; }
public Boolean partial_match { get; set; }
public string[] types { get; set; }
}
public class address_component
{
public String long_name { get; set; }
public String short_name { get; set; }
public String[] types { get; set; }
}
public class Geometry
{
public bounds_viewport bounds { get; set; }
public LatLong location { get; set; }
public String location_type { get; set; }
}
public class bounds_viewport
{
public LatLong northeast { get; set; }
public LatLong southwest { get; set; }
}
public class LatLong
{
public double lon { get; set; }
public double lat { get; set; }
}
在我的表单按钮上单击我使用此代码。
var json = new WebClient().DownloadString("url");
GoogleGeoCodeResponse test = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(json);
MessageBox.Show(test.results[0].geometry.location.lat + " / " + test.results[0].geometry.location.lon);
然后我想看看我是否得到了正确的信息,但我得到了:
“错误 1 可访问性不一致:属性类型 'WindowsFormsApplication1.results[]' 的可访问性低于属性 'WindowsFormsApplication1.GoogleGeoCodeResponse.results'”
我看过一堆有同样问题的帖子,但他们总是有一些私人或内部的东西会导致这个错误。如您所见,我声明的所有内容都是公开的。
我不明白为什么类中的公共内容比类本身更易于访问。
我对 C# 和 JSON 还很陌生,所以这可能非常简单。
【问题讨论】:
-
这些是嵌套类吗? (它们是在另一个类中声明的吗?)
-
我已经在我的表单类内部和外部尝试过,它没有改变任何东西。我需要创建一个全新的类并在其中添加代码吗?
-
我意识到我并没有真正回答你的问题,但是是的,它们在我的表单类中。
-
观察窗口中是否可见错误?如果是这样,您似乎已将
results变量的范围设置为WindowsFormsApplication1放在手表中。 -
您的 form 是否已公开?如果它被声明为
internal(这是默认值),那么其中的任何公共类也将具有internal的有效可见性。