【问题标题】:C# serialize json from Twitter without data memberC# 序列化来自 Twitter 的 json 没有数据成员
【发布时间】:2023-03-15 08:36:01
【问题描述】:

我正在使用 C# 编写自己的小型 Twitter 应用程序。 我已经设法序列化来自https://api.twitter.com/1/followers/ids.json?cursor=-1&screen_name= 的 json 数据 它返回如下内容:

{ "ids" : [ 401295021,
  506271294,
  14405250,
  25873220
],
"next_cursor" : 0,
"next_cursor_str" : "0",
"previous_cursor" : 0,
"previous_cursor_str" : "0"
}

使用这个类我可以序列化它:

    [DataContract]
    public class TwitterFollowers
    {
        [DataMember(Name = "ids")]
        public IList<int> AccountIDs { get; set; }
    }

现在我想获取关注者的屏幕名称,所以我使用这个 url: http://api.twitter.com/1/users/lookup.json?user_id=

这个 json 看起来像这样:

[ { "contributors_enabled" : false,
"created_at" : "Wed Apr 16 06:30:52 +0000 2008",
"default_profile" : false,
"default_profile_image" : false,
"description" : "",
"utc_offset" : -25200,
"verified" : false
},
{ "contributors_enabled" : false,
"created_at" : "Tue Mar 04 12:31:57 +0000 2008",
"default_profile" : true,
"default_profile_image" : false,
"description" : "",
"utc_offset" : 3600,
"verified" : false
}
]

如您所见,数组立即开始,无需命名。 我的班级应该如何序列化这个?

这个我试过了,但是不行:

    [DataContract]
    public class TwitterProfiles
    {
        [DataMember(Name = "")]
        public IList<TwitterProfile> Profiles { get; set; }
    }

    [DataContract]
    public class TwitterProfile
    {
        [DataMember(Name = "lang")]
        public string Language { get; set; }

        [DataMember(Name = "location")]
        public string Location { get; set; }

        [DataMember(Name = "name")]
        public string Name { get; set; }

        [DataMember(Name = "screen_name")]
        public string ScreenName { get; set; }

        [DataMember(Name = "url")]
        public string URL { get; set; }
    }

【问题讨论】:

  • 您是否尝试过从 DataMember 属性中删除“名称”属性?意思是,[DataMember] public IList Profiles { get;放; }

标签: c# json serialization twitter


【解决方案1】:

您是否尝试过从 DataMember 属性中移除“名称”属性?含义:

[DataMember]
public IList<TwitterProfile> Profiles { get; set; }

如果这不起作用,您还可以选择从 api 调用中获取输出,并将其格式化为您知道可以反序列化的 json 字符串:

string.Format("{{\"profiles\":{0}}}", GetJsonResponse())

【讨论】:

    【解决方案2】:

    您提供的示例与TwitterProfile 类的成员不匹配。尽管如此,您不需要帮助类来处理数组。您可以直接在反序列化时直接定位数组类型。

    这是一个可用于测试的代码示例。我已经删除了您的 TwitterProfiles 类并将 TwitterProfile 剥离为一个名为 CreatedAt 的字段。

    请注意以下源代码中的以下行:

    DataContractJsonSerializer js = 
        new DataContractJsonSerializer(typeof(TwitterProfile[]));
    

    剩下的就到这里了。

    using System.Windows.Forms;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Json;
    using System.IO;
    
    namespace TwProfileJson
    {
        class Program
        {
            [STAThread]
            static void Main(string[] args)
            {
                OpenFileDialog dlg = new OpenFileDialog();
                if (dlg.ShowDialog() != DialogResult.OK) { return; }
                string json = System.IO.File.ReadAllText(dlg.FileName);
    
                using(MemoryStream stm = new MemoryStream(Encoding.UTF8.GetBytes(json)))
                {
                    DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(TwitterProfile[]));
                    TwitterProfile[] pps = (TwitterProfile[])js.ReadObject(stm);
    
                    foreach(TwitterProfile p in pps)
                    {
                        Console.WriteLine(p.CreatedAt);
                    }
                }
    
                Console.ReadKey();
            }
    
            [DataContract]
            public class TwitterProfile
            {
                [DataMember(Name = "created_at")]
                public string CreatedAt { get; set; }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-09
      • 1970-01-01
      • 2018-07-07
      • 2011-10-22
      • 2010-10-22
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      相关资源
      最近更新 更多