我被困在解析 JSon 并且无法弄清楚。它在 IDictionary 的数组中给了我 NULL。这是我所拥有的:
{
"response" : {
"method" : "my.current.method",
"result" : {
"current_calls" : {
"current_call" : [
{
"provider" : "ABC",
"start_time" : "2014-11-30 02:24:52",
"duration" : "5",
"to_caller_id_number" : "800",
"state" : "ivr",
"from_caller_id_name" : "<unknown>",
"to_caller_id_name" : "Main Answer Queue",
"format" : "ulaw",
"from_caller_id_number" : "1234567890",
"id" : "SIP/1234567890-08682a00"
},
{
"provider" : "ThinkTel",
"start_time" : "2014-11-30 02:24:50",
"duration" : "7",
"to_caller_id_number" : "800",
"state" : "ivr",
"from_caller_id_name" : "<unknown>",
"to_caller_id_name" : "Main Answer Queue",
"format" : "ulaw",
"from_caller_id_number" : "0123456789",
"id" : "SIP/0123456789-08681350"
}
],
"total_items" : "2"
}
}
}
}
然后我的 C# 代码中有以下内容:
public class Data
{
public Response response { get; set; }
}
public class Response
{
public string method { get; set; }
public Result result { get; set; }
}
public class Result
{
public CurrentCalls current_calls { get; set; }
}
public class CurrentCalls
{
public List<IDictionary<string, Current_call>> current_calls { get; set; }
public int total_items { get; set; }
}
public class Current_call
{
public string provider { get; set; }
public DateTime start_time { get; set; }
public int duration { get; set; }
public string to_caller_id_number { get; set; }
public string state { get; set; }
public string from_caller_id_name { get; set; }
public string to_caller_id_name { get; set; }
public string format { get; set; }
public string from_caller_id_number { get; set; }
public string id { get; set; }
}
public class Data
{
public Response response { get; set; }
}
public class Response
{
public string method { get; set; }
public Result result { get; set; }
}
public class Result
{
public CurrentCalls current_calls { get; set; }
}
public class CurrentCalls
{
public List<IDictionary<string, Current_call>> current_calls { get; set; }
public int total_items { get; set; }
}
public class Current_call
{
public string provider { get; set; }
public DateTime start_time { get; set; }
public int duration { get; set; }
public string to_caller_id_number { get; set; }
public string state { get; set; }
public string from_caller_id_name { get; set; }
public string to_caller_id_name { get; set; }
public string format { get; set; }
public string from_caller_id_number { get; set; }
public string id { get; set; }
}
我像这样反序列化它:
allCalls = JsonConvert.DeserializeObject<Data>(json);
然后我试图在下面使用它:
if (parser.allCalls.response.result.current_calls.current_calls != null)
{
foreach (var defindex in parser.allCalls.response.result.current_calls.current_calls)
{
foreach (var call in defindex) {
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\A\\Testing\\test" + i + ".txt");
file.WriteLine("Current call:" );
file.WriteLine("\t Provider: " + call.Value.provider);
file.WriteLine("\t Start Tine: " + call.Value.start_time);
file.WriteLine("\t Duration: " + call.Value.duration);
file.WriteLine("\t To Caller: " + call.Value.to_caller_id_number);
file.WriteLine("\t State: " + call.Value.state);
file.WriteLine("\t From: " + call.Value.from_caller_id_name);
file.WriteLine("\t To Name: " + call.Value.to_caller_id_name);
file.WriteLine("\t Format: " + call.Value.format);
file.WriteLine("\t From Caller ID: " + call.Value.from_caller_id_number);
file.WriteLine("\t ID: " + call.Value.id);
file.Close();
}
}
}
所以 Total Items 的值为 2 ok。我似乎无法计算 IDictionary 的数组。它是空的。请帮忙。(这是使用 Newtonsoft 顺便说一句)