我正在尝试解析来自facebook c# sdk的json数据。我试图解析的json数据可以在facebook:https://graph.facebook.com/search?q=coffee&type=place¢er=37.76,-122.427&distance=1000&access_token=AAAAAAITEghMBACQupPhpGCGi1Jce7eMfZCzt9GlpZBdhz3PlGCyHKNZB1r4FHgd9mgpm8W3g4Adpy9jJjFrsDuxcu3pE4uRT1lbIQjYKgZDZD上看到
我下面的代码将弹出一个消息框,显示这个json对象的第一个维度,但是,正如您所看到的,每个项目中都有第二个维度,它提供了经度和纬度等位置信息。我正在努力寻找一个例子,说明如何使用WP7 C#实现这一点(互联网上的大多数例子使用的库在WP7上不可用)。
fbClient.GetCompleted += (o, er) =>
{
if (er.Error == null)
{
var result = (IDictionary<string, object>)er.GetResultData();
Dispatcher.BeginInvoke(() =>
{
foreach (var item in (JsonArray)result["data"])
{
//message box for testing purposes
MessageBox.Show((string)((JsonObject)item)["name"]);
}
});
}
});有没有人能提供一个简单的例子?
谢谢。
发布于 2012-05-27 16:53:17
因为您使用的是FacebookSDK,所以不需要直接使用json。只需将JsonObjects转换为IDictionary,并像字典一样使用它:
//think better use IEnumerable<object>, because you don't really need JSON array
foreach (var item in (IEnumerable<object>)result["data"])
{
var name = (item as IDictionary<string, object>)["name"];
//message box for testing purposes
MessageBox.Show(name);
}因此,您可以像IEnumerable<object>一样使用JsonArray,也可以像IDictionary<string, object>一样使用JsonObject
回答你的问题:
var item1 = (IDictionary<string, object>)item;
var location = ((IDictionary<string, object>)(item1)["location"]);
var long = location["longitude"];或者,您可以使用JSON来完成:
var location = ((JsonObject)((JsonObject)item)["location"]);
var long = location["longitude"];https://stackoverflow.com/questions/10772474
复制相似问题