'System.Collections.Generic.KeyValuePair< string,object>'不包含“工作"的定义 [英] 'System.Collections.Generic.KeyValuePair<string,object>' does not contain a definition for 'work'

查看:179
本文介绍了'System.Collections.Generic.KeyValuePair< string,object>'不包含“工作"的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    namespace WebApplication1.Site
{
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        var accessToken = "" // Token
        var client = new FacebookClient(accessToken);
        dynamic myInfo = client.Get("me/friends", new { fields = "name,id,work" });

        foreach (dynamic friend in myInfo)
        {
            foreach (dynamic work in friend.work ?? new[] { new { employer = new { name = string.Empty }, position = new { name = string.Empty } } })
                {
                    Response.Write("Employer: " + work.employer.name);
                }

        }


    }
}
}

我在第21行出现以下错误.我无法弄清楚是什么原因造成的.

I am getting the following error on line 21. I cannot figure out what is causing it.

"System.Collections.Generic.KeyValuePair"不包含工作"的定义

'System.Collections.Generic.KeyValuePair' does not contain a definition for 'work'

示例JSON从Facebook GraphAPI返回.这只是前三个朋友.我正在解析的好友接近4000,显然这为数据的结构提供了一些上下文:

Sample JSON Return from the Facebook GraphAPI. This is only the first three friends. There are closer to 4000 friends I am parsing, obviously this gives some context for the structure of the data:

    {
      "data": [
{
  "name": "Mia xxx", 
  "id": "11381", 
  "work": [
    {
      "employer": {
        "id": "100982923276720", 
        "name": "New-York Historical Society"
      }, 
      "location": {
        "id": "108424279189115", 
        "name": "New York, New York"
      }
    }
  ]
}, 
{
  "name": "Leilah xxx", 
  "id": "1133"
}, 
{
  "name": "xxx, 
  "id": "1231", 
  "work": [
    {
      "employer": {
        "id": "104362369673437", 
        "name": "Bye Bye Liver: The Philadelphia Drinking Play"
      }, 
      "location": {
        "id": "101881036520836", 
        "name": "Philadelphia, Pennsylvania"
      }, 
      "position": {
        "id": "121113421241569", 
        "name": "Actress/Bartender"
      }, 
      "description": "A sketch comedy/improv show every Saturday night at Downey's on South & Front. Come thirsty!", 
      "start_date": "2011-09"
    }, 
    {
      "employer": {
        "id": "100952634348", 
        "name": "Act II Playhouse"
      }, 
      "location": {
        "id": "109249869093538", 
        "name": "Ambler, Pennsylvania"
      }, 
      "position": {
        "id": "125578900846788", 
        "name": "My Fair Lady"
      }, 
      "description": "11 actor version of the classic musical.", 
      "start_date": "0000-00"
    }, 

推荐答案

依赖dynamic的另一种方法是使用JSON.net捕获和解析JSON,它旨在查询JSON数据,实际上比使用dynamic

An alternative to relying on the dynamic is to capture and parse the JSON with JSON.net, it's designed for querying json data and is really much safer than using dynamic

http://json.codeplex.com/

反序列化为类:

http://dotnetbyexample.blogspot.ca/2012/02/json-deserialization-with-jsonnet-class.html

这篇关于'System.Collections.Generic.KeyValuePair< string,object>'不包含“工作"的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆