将包含对象列表的JSON响应反序列化为C#类
我正在使用它返回一个提要列表。响应JSON的类型为:将包含对象列表的JSON响应反序列化为C#类,c#,json,serialization,json.net,C#,Json,Serialization,Json.net,我正在使用它返回一个提要列表。响应JSON的类型为: { "authenticated": true, "user": "sathyabhat", "feeds": {}, "flat_folders": { }, result: "ok" } 其中提要:为 "feeds": { "96705": { "feed_address": "http://www.linuxhaxor.net/",
{
"authenticated": true,
"user": "sathyabhat",
"feeds": {},
"flat_folders": { },
result: "ok"
}
其中提要:
为
"feeds": {
"96705": {
"feed_address": "http://www.linuxhaxor.net/",
"updated": "8492 hours",
"favicon_text_color": null,
"subs": 35,
"feed_link": "http://www.linuxhaxor.net/",
"favicon_fetching": true,
"nt": 0,
"updated_seconds_ago": 30573336,
"num_subscribers": 35,
"feed_title": "LinuxHaxor.net",
"favicon_fade": null,
"exception_type": "feed",
"exception_code": 503,
"favicon_color": null,
"active": true,
"ng": 0,
"feed_opens": 0,
"id": 96705,
"ps": 0,
"has_exception": true
},
"768840": {
"feed_address": "http://feeds.feedburner.com/PathikShahDotCom",
"updated": "3 hours",
"favicon_text_color": "black",
"subs": 1,
"feed_link": "http://www.pathikshah.com/blog",
"favicon_fetching": false,
"nt": 0,
"updated_seconds_ago": 13043,
"num_subscribers": 1,
"feed_title": "Pathik Shah",
"favicon_fade": "769456",
"favicon_color": "b2d092",
"active": true,
"ng": 0,
"feed_opens": 0,
"id": 768840,
"ps": 0
},
"768842": {
"feed_address": "http://feeds.preshit.net/preshit/blog",
"updated": "3 hours",
"favicon_text_color": null,
"subs": 1,
"feed_link": "http://preshit.net",
"favicon_fetching": false,
"nt": 3,
"updated_seconds_ago": 13536,
"num_subscribers": 1,
"feed_title": "Preshit Deorukhkar",
"favicon_fade": null,
"favicon_color": null,
"active": true,
"ng": 0,
"feed_opens": 1,
"id": 768842,
"ps": 0
},
"768843": {
"feed_address": "http://quizwith.net/feed/",
"updated": "3 hours",
"favicon_text_color": "white",
"subs": 1,
"feed_link": "http://quizwith.net",
"favicon_fetching": false,
"nt": 0,
"updated_seconds_ago": 11617,
"num_subscribers": 1,
"feed_title": "quizwith.net",
"favicon_fade": "c22900",
"favicon_color": "fe6501",
"active": true,
"ng": 0,
"feed_opens": 0,
"id": 768843,
"ps": 0
}
[……如此……]
}
所以本质上,它是一个提要对象列表。现在,我试图使用将其反序列化为C#对象,但我的类成员中填充了空值
我的课堂是以我得到的回应为模型的:
public class FeedResponse
{
public string authenticated { get; set; }
public string user { get; set; }
public List<feed> feeds { get; set; }
public string flat_folders { get; set; }
public string result { get; set; }
}
}
这是尝试反序列化(使用RestSharp)的行:
返回执行(请求);
显然,在尝试解析提要响应的JSON时出现了一些问题
有没有关于我做错了什么的指针?提要是一个
字典
而不是列表
(有键)
编辑:再详细一点,json中的对象列表如下所示:
[{ name: 'elem1'},
{ name: 'elem2'},
{ name: 'elem3'}]
edit2:全样本工作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Script.Serialization;
namespace JSON {
class Program
{
static void Main(string[] args)
{
var json = @"{
""authenticated"": true,
""user"": ""sathyabhat"",
""feeds"": {
""96705"": {
""feed_address"": ""http://www.linuxhaxor.net/"",
""updated"": ""8492 hours"",
""favicon_text_color"": null,
""subs"": 35,
""feed_link"": ""http://www.linuxhaxor.net/"",
""favicon_fetching"": true,
""nt"": 0,
""updated_seconds_ago"": 30573336,
""num_subscribers"": 35,
""feed_title"": ""LinuxHaxor.net"",
""favicon_fade"": null,
""exception_type"": ""feed"",
""exception_code"": 503,
""favicon_color"": null,
""active"": true,
""ng"": 0,
""feed_opens"": 0,
""id"": 96705,
""ps"": 0,
""has_exception"": true
},
""768840"": {
""feed_address"": ""http://feeds.feedburner.com/PathikShahDotCom"",
""updated"": ""3 hours"",
""favicon_text_color"": ""black"",
""subs"": 1,
""feed_link"": ""http://www.pathikshah.com/blog"",
""favicon_fetching"": false,
""nt"": 0,
""updated_seconds_ago"": 13043,
""num_subscribers"": 1,
""feed_title"": ""Pathik Shah"",
""favicon_fade"": ""769456"",
""favicon_color"": ""b2d092"",
""active"": true,
""ng"": 0,
""feed_opens"": 0,
""id"": 768840,
""ps"": 0
},
""768842"": {
""feed_address"": ""http://feeds.preshit.net/preshit/blog"",
""updated"": ""3 hours"",
""favicon_text_color"": null,
""subs"": 1,
""feed_link"": ""http://preshit.net"",
""favicon_fetching"": false,
""nt"": 3,
""updated_seconds_ago"": 13536,
""num_subscribers"": 1,
""feed_title"": ""Preshit Deorukhkar"",
""favicon_fade"": null,
""favicon_color"": null,
""active"": true,
""ng"": 0,
""feed_opens"": 1,
""id"": 768842,
""ps"": 0
},
""768843"": {
""feed_address"": ""http://quizwith.net/feed/"",
""updated"": ""3 hours"",
""favicon_text_color"": ""white"",
""subs"": 1,
""feed_link"": ""http://quizwith.net"",
""favicon_fetching"": false,
""nt"": 0,
""updated_seconds_ago"": 11617,
""num_subscribers"": 1,
""feed_title"": ""quizwith.net"",
""favicon_fade"": ""c22900"",
""favicon_color"": ""fe6501"",
""active"": true,
""ng"": 0,
""feed_opens"": 0,
""id"": 768843,
""ps"": 0
}
},
""flat_folders"": { },
result: ""ok"" }";
var jsonSerializer = new JavaScriptSerializer();
var response = jsonSerializer.Deserialize<FeedResponse(json);
Console.Write(jsonSerializer.Serialize(response));
Console.ReadKey();
}
}
public class FeedResponse
{
public bool authenticated { get; set; }
public string user { get; set; }
public Dictionary<string,feed> feeds { get; set; }
public object flat_folders { get; set; }
public string result { get; set; }
}
public class feed
{
public string feed_address { get; set; }
public string updated { get; set; }
public string favicon_text_color { get; set; }
public int subs { get; set; }
public string feed_link { get; set; }
public bool favicon_fetching { get; set; }
public string nt { get; set; }
public string updated_seconds_ago { get; set; }
public int num_subscribers { get; set; }
public string feed_title { get; set; }
public string favicon_fade { get; set; }
public string exception_type { get; set; }
public string exception_code { get; set; }
public string favicon_color { get; set; }
public bool active { get; set; }
public string ng { get; set; }
public int feed_opens { get; set; }
public int id { get; set; }
public bool has_exception { get; set; }
} }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web.Script.Serialization;
名称空间JSON{
班级计划
{
静态void Main(字符串[]参数)
{
var json=@”{
“已验证”:正确,
“用户”:“sathyabhat”,
“提要”:{
""96705"": {
“源地址”:http://www.linuxhaxor.net/"",
“已更新”:“8492小时”,
“favicon\u text\u color”:空,
“subs”:35,
“源链接”:http://www.linuxhaxor.net/"",
“favicon_fetching”:对,
“nt”:0,
“在几秒钟前更新”:3057336,
“用户数”:35,
“”提要标题“”:“”LinuxHaxor.net“”,
“favicon_fade”:null,
“”异常类型“”:“”提要“”,
“例外情况代码”:503,
“favicon_color”:空,
“活动”:是,
“ng”:0,
“”源_打开“”:0,
“id”:96705,
“ps”:0,
“”有异常“”:true
},
""768840"": {
“源地址”:http://feeds.feedburner.com/PathikShahDotCom"",
“已更新”:“3小时”,
“favicon\u text\u color”:“black”,
“subs”:1,
“源链接”:http://www.pathikshah.com/blog"",
“favicon_fetching”:false,
“nt”:0,
“在几秒钟前更新”:13043,
“用户数”:1,
“feed_title”:“Pathik Shah”,
“favicon_fade”:“769456”,
“favicon_color”:“b2d092”,
“活动”:是,
“ng”:0,
“”源_打开“”:0,
“id”:768840,
“ps”:0
},
""768842"": {
“源地址”:http://feeds.preshit.net/preshit/blog"",
“已更新”:“3小时”,
“favicon\u text\u color”:空,
“subs”:1,
“源链接”:http://preshit.net"",
“favicon_fetching”:false,
“nt”:3,
“在几秒钟前更新”:13536,
“用户数”:1,
“feed_title”:“Preshit Deorukhkar”,
“favicon_fade”:null,
“favicon_color”:空,
“活动”:是,
“ng”:0,
“feed_打开”:1,
“id”:768842,
“ps”:0
},
""768843"": {
“源地址”:http://quizwith.net/feed/"",
“已更新”:“3小时”,
“favicon\u text\u color”:“white”,
“subs”:1,
“源链接”:http://quizwith.net"",
“favicon_fetching”:false,
“nt”:0,
“在几秒钟前更新”:11617,
“用户数”:1,
“”提要标题“”:“”quizwith.net“”,
“favicon_fade”:“c22900”,
“favicon_color”:“fe6501”,
“活动”:是,
“ng”:0,
“”源_打开“”:0,
“id”:768843,
“ps”:0
}
},
“平面文件夹”“:{},
结果:“确定”};
var jsonSerializer=新的JavaScriptSerializer();
var response=jsonSerializer.Deserialize使用jsonreader,使用json.net,您的示例应该如下所示:
public class Feeds
{
public bool authenticated;
public string user;
public Dictionary<string, Feed> feeds;
public void ReadJson(JsonReader reader)
{
while (reader.Read())
{
if (reader.TokenType == JsonToken.EndObject)
break;
if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "authenticated")
{
reader.Read();
authenticated = (bool)reader.Value;
}
else if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "user")
{
reader.Read();
user = (string)reader.Value;
}
else if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "feeds")
{
feeds = new Dictionary<string, Feed>();
while (reader.Read())
{
if (reader.TokenType == JsonToken.EndObject)
break;
if (reader.TokenType == JsonToken.PropertyName)
{
string key = (string)reader.Value;
feeds.Add(key, Feed.ReadFromJson(reader));
}
}
}
}
}
public static Feeds ReadFromJson(Stream stream)
{
using (StreamReader sr = new StreamReader(stream))
{
using (Newtonsoft.Json.JsonReader jsonReader = new Newtonsoft.Json.JsonTextReader(sr))
{
Feeds feeds = new Feeds();
feeds.ReadJson(jsonReader);
return feeds;
}
}
}
}
public class Feed
{
public string feed_address;
public string updated;
public void ReadJson(JsonReader reader)
{
while (reader.Read())
{
if (reader.TokenType == JsonToken.EndObject)
break;
if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "feed_address")
{
reader.Read();
feed_address = (string)reader.Value;
}
else if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "updated")
{
reader.Read();
updated = (string)reader.Value;
}
}
}
public static Feed ReadFromJson(JsonReader reader)
{
Feed feed = new Feed();
feed.ReadJson(reader);
return feed;
}
}
公共类提要
{
公共bool认证;
公共字符串用户;
公共词典提要;
public void ReadJson(JsonReader)
{
while(reader.Read())
{
if(reader.TokenType==JsonToken.EndObject)
打破
if(reader.TokenType==JsonToken.PropertyName&&(string)reader.Value==authenticated)
{
reader.Read();
authenticated=(bool)reader.Value;
}
else if(reader.TokenType==JsonToken.PropertyName&&(string)reader.Value==user)
{
reader.Read();
user=(字符串)reader.Value;
}
else if(reader.TokenType==JsonToken.PropertyName&&(string)reader.Value==feed)
{
feed=新字典();
while(reader.Read())
{
if(reader.TokenType==JsonToken.EndObject)
打破
if(reader.TokenType==JsonToken.PropertyName)
{
字符串键=(字符串)reader.Value;
Feed.Add(key,Feed.ReadFromJson(reader));
}
}
}
}
}
公共静态提要ReadFromJson(流)
{
使用(StreamReader sr=新StreamReader(stream))
{
使用(Newtonsoft.Json.JsonReader JsonReader=new Newtonsoft.Json.JsonTextReader(sr))
{
feed=新feed();
ReadJson(jsonReader);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Script.Serialization;
namespace JSON {
class Program
{
static void Main(string[] args)
{
var json = @"{
""authenticated"": true,
""user"": ""sathyabhat"",
""feeds"": {
""96705"": {
""feed_address"": ""http://www.linuxhaxor.net/"",
""updated"": ""8492 hours"",
""favicon_text_color"": null,
""subs"": 35,
""feed_link"": ""http://www.linuxhaxor.net/"",
""favicon_fetching"": true,
""nt"": 0,
""updated_seconds_ago"": 30573336,
""num_subscribers"": 35,
""feed_title"": ""LinuxHaxor.net"",
""favicon_fade"": null,
""exception_type"": ""feed"",
""exception_code"": 503,
""favicon_color"": null,
""active"": true,
""ng"": 0,
""feed_opens"": 0,
""id"": 96705,
""ps"": 0,
""has_exception"": true
},
""768840"": {
""feed_address"": ""http://feeds.feedburner.com/PathikShahDotCom"",
""updated"": ""3 hours"",
""favicon_text_color"": ""black"",
""subs"": 1,
""feed_link"": ""http://www.pathikshah.com/blog"",
""favicon_fetching"": false,
""nt"": 0,
""updated_seconds_ago"": 13043,
""num_subscribers"": 1,
""feed_title"": ""Pathik Shah"",
""favicon_fade"": ""769456"",
""favicon_color"": ""b2d092"",
""active"": true,
""ng"": 0,
""feed_opens"": 0,
""id"": 768840,
""ps"": 0
},
""768842"": {
""feed_address"": ""http://feeds.preshit.net/preshit/blog"",
""updated"": ""3 hours"",
""favicon_text_color"": null,
""subs"": 1,
""feed_link"": ""http://preshit.net"",
""favicon_fetching"": false,
""nt"": 3,
""updated_seconds_ago"": 13536,
""num_subscribers"": 1,
""feed_title"": ""Preshit Deorukhkar"",
""favicon_fade"": null,
""favicon_color"": null,
""active"": true,
""ng"": 0,
""feed_opens"": 1,
""id"": 768842,
""ps"": 0
},
""768843"": {
""feed_address"": ""http://quizwith.net/feed/"",
""updated"": ""3 hours"",
""favicon_text_color"": ""white"",
""subs"": 1,
""feed_link"": ""http://quizwith.net"",
""favicon_fetching"": false,
""nt"": 0,
""updated_seconds_ago"": 11617,
""num_subscribers"": 1,
""feed_title"": ""quizwith.net"",
""favicon_fade"": ""c22900"",
""favicon_color"": ""fe6501"",
""active"": true,
""ng"": 0,
""feed_opens"": 0,
""id"": 768843,
""ps"": 0
}
},
""flat_folders"": { },
result: ""ok"" }";
var jsonSerializer = new JavaScriptSerializer();
var response = jsonSerializer.Deserialize<FeedResponse(json);
Console.Write(jsonSerializer.Serialize(response));
Console.ReadKey();
}
}
public class FeedResponse
{
public bool authenticated { get; set; }
public string user { get; set; }
public Dictionary<string,feed> feeds { get; set; }
public object flat_folders { get; set; }
public string result { get; set; }
}
public class feed
{
public string feed_address { get; set; }
public string updated { get; set; }
public string favicon_text_color { get; set; }
public int subs { get; set; }
public string feed_link { get; set; }
public bool favicon_fetching { get; set; }
public string nt { get; set; }
public string updated_seconds_ago { get; set; }
public int num_subscribers { get; set; }
public string feed_title { get; set; }
public string favicon_fade { get; set; }
public string exception_type { get; set; }
public string exception_code { get; set; }
public string favicon_color { get; set; }
public bool active { get; set; }
public string ng { get; set; }
public int feed_opens { get; set; }
public int id { get; set; }
public bool has_exception { get; set; }
} }
public class Feeds
{
public bool authenticated;
public string user;
public Dictionary<string, Feed> feeds;
public void ReadJson(JsonReader reader)
{
while (reader.Read())
{
if (reader.TokenType == JsonToken.EndObject)
break;
if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "authenticated")
{
reader.Read();
authenticated = (bool)reader.Value;
}
else if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "user")
{
reader.Read();
user = (string)reader.Value;
}
else if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "feeds")
{
feeds = new Dictionary<string, Feed>();
while (reader.Read())
{
if (reader.TokenType == JsonToken.EndObject)
break;
if (reader.TokenType == JsonToken.PropertyName)
{
string key = (string)reader.Value;
feeds.Add(key, Feed.ReadFromJson(reader));
}
}
}
}
}
public static Feeds ReadFromJson(Stream stream)
{
using (StreamReader sr = new StreamReader(stream))
{
using (Newtonsoft.Json.JsonReader jsonReader = new Newtonsoft.Json.JsonTextReader(sr))
{
Feeds feeds = new Feeds();
feeds.ReadJson(jsonReader);
return feeds;
}
}
}
}
public class Feed
{
public string feed_address;
public string updated;
public void ReadJson(JsonReader reader)
{
while (reader.Read())
{
if (reader.TokenType == JsonToken.EndObject)
break;
if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "feed_address")
{
reader.Read();
feed_address = (string)reader.Value;
}
else if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "updated")
{
reader.Read();
updated = (string)reader.Value;
}
}
}
public static Feed ReadFromJson(JsonReader reader)
{
Feed feed = new Feed();
feed.ReadJson(reader);
return feed;
}
}
JObject rootObj = JsonConvert.DeserializeObject(jstr) as JObject;
Console.WriteLine(
"Authenticated:" + rootObj["authenticated"] +
" USER:" + rootObj["user"] +
" RESULT:" + rootObj["result"]);
foreach (JProperty feed in rootObj["feeds"])
{
Console.WriteLine(feed.Name + " " + feed.Value["feed_address"]);
}