var str = @"{'status':0, 'array':[[+8613000000000,1905091845400259790]], 'success':1, 'fail':0}";
这是一个国际短信平台发送短信后的返回数据,我用Json.NET反序列化时用任何已有对象都不能成功
class Program { static void Main(string[] args) { var str = @"{'status':0, 'array':[[+8613000000000,1905091845400259790]], 'success':1, 'fail':0}"; var result= JsonConvert.DeserializeObject<Response>(str); Console.ReadLine(); } } public class Response { [JsonProperty("status", NullValueHandling = NullValueHandling.Ignore)] public int Status { get; set; } [JsonProperty("success", NullValueHandling = NullValueHandling.Ignore)] public int Success { get; set; } [JsonProperty("fail", NullValueHandling = NullValueHandling.Ignore)] public int Fail { get; set; } [JsonProperty("array",TypeNameHandling =TypeNameHandling.All)] public object Array { get; set; } }
去掉加号,将 Array 定义为二维数组
class Program
{
static void Main(string[] args)
{
var str = @"{'status':0, 'array':[[+8613000000000,1905091845400259790]], 'success':1, 'fail':0}";
var result = JsonConvert.DeserializeObject<Response>(str.Replace("[+", "["));
Console.WriteLine(result.Array[0][0]);
Console.ReadKey();
}
}
public class Response
{
[JsonProperty("status", NullValueHandling = NullValueHandling.Ignore)]
public int Status { get; set; }
[JsonProperty("success", NullValueHandling = NullValueHandling.Ignore)]
public int Success { get; set; }
[JsonProperty("fail", NullValueHandling = NullValueHandling.Ignore)]
public int Fail { get; set; }
[JsonProperty("array", TypeNameHandling = TypeNameHandling.All)]
public string[][] Array { get; set; }
}
var result = JsonConvert.DeserializeObject<Response>(Regex.Replace(result, "(\\+?\\d+)", "\"$1\"", RegexOptions.Multiline));
感谢Dudu大神提供的思路