C# C从Twitter序列化json而不使用数据成员
我正在用C语言开发自己的小型Twitter应用程序。 我已经成功地从中序列化了json数据 它返回如下内容:C# C从Twitter序列化json而不使用数据成员,c#,json,serialization,twitter,C#,Json,Serialization,Twitter,我正在用C语言开发自己的小型Twitter应用程序。 我已经成功地从中序列化了json数据 它返回如下内容: { "ids" : [ 401295021, 506271294, 14405250, 25873220 ], "next_cursor" : 0, "next_cursor_str" : "0", "previous_cursor" : 0, "previous_cursor_str" : "0" } [ { "contributors_enabled" : false,
{ "ids" : [ 401295021,
506271294,
14405250,
25873220
],
"next_cursor" : 0,
"next_cursor_str" : "0",
"previous_cursor" : 0,
"previous_cursor_str" : "0"
}
[ { "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 TwitterFollowers
{
[DataMember(Name = "ids")]
public IList<int> AccountIDs { get; set; }
}
如您所见,阵列立即启动,而不命名。
我的类应该如何序列化它
我试过这个,但不起作用:
[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属性中删除Name属性?意思是:
[DataMember]
public IList<TwitterProfile> Profiles { get; set; }
您提供的示例与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; }
}
}
}
您是否尝试过一起从DataMember属性中删除Name属性?意思是,[DataMember]公共IList配置文件{get;set;}
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; }
}
}
}