从API返回的JSON字符串:
{"status":0,"data":{"respondents":[{"date_modified":"2015-05-18 19:08:07","respondent_id":"3973604066"},{"date_modified":"2015-05-11 20:00:30","respondent_id":"3958700049"}],"page":1,"page_size":1000}}
如果这是XML,我会使用LINQ并将其作为List存储在List中。
我确信如果我能得到如何处理这个问题的答案,我可以将它应用到更高的层次。
我使用的是.net 3.5或4.0。
我无法通过CCB安装Json.Net。
感谢。
这是我的代码:
课程计划 {
public class StatusObj
{
public int status {get; set; }
public List<DataObj> data {get; set;}
}
public class DataObj
{
public List<RespondentObj> respondents { get; set; }
public int page {get; set;}
public int page_size {get; set;}
}
public class RespondentObj
{
public DateTime date_modified {get; set;}
public int respondent_id {get; set;}
}
static void Main(string[] args)
{
Console.WriteLine(DateTime.Now.ToString());
Console.WriteLine("Pulling WSDL");
// Removed the API Pull since I don't have issue there
var reader = new StreamReader(responseStream, Encoding.UTF8);
string json_returned = reader.ReadToEnd();
// "{\"status\":0,\"data\":{\"respondents\":[{\"date_modified\":\"2015-05-18 19:08:07\",\"respondent_id\":\"3973604066\"},{\"date_modified\":\"2015-05-11 20:00:30\",\"respondent_id\":\"3958700049\"}],\"page\":1,\"page_size\":1000}}";
Console.WriteLine(json_returned);
//This is what Cody Suggested
var deserializer = new JavaScriptSerializer();
var results = deserializer.Deserialize<StatusObj>(json_returned);
foreach (var x in results.data)
{
foreach (var y in x.respondents)
{
Console.WriteLine(x.page);
Console.WriteLine(y.respondent_id);
Console.WriteLine(y.date_modified);
}
}
Console.ReadLine();
}
}
答案 0 :(得分:2)
我会使用内置的JavaScriptSerializer和匹配的类。所以你会有这样的事情:
public class StatusObj{
public int status {get; set; }
public DataObj data { get; set; }
}
public class DataObj {
public List<RespondentObj> respondents {get set;}
public int page {get; set;}
public int page_size {get; set;}
}
public class RespondentObj{
public DateTime date_modified {get; set;}
public Int64 respondent_id {get; set;}
}
然后你可以像这样反序列化你的流:
var deserializer = new JavaScriptSerializer();
var results = deserializer.Deserialize<StatusObj>(stream);