首页新闻找找看学习计划

如何反序列化下面的json字符串

0
悬赏园豆:20 [已解决问题] 解决于 2019-05-10 10:45

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; }
    }
supperAdministrator的主页 supperAdministrator | 初学一级 | 园豆:12
提问于:2019-05-10 09:50
< >
分享
最佳答案
0

去掉加号,将 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; }
}
收获园豆:20
dudu | 高人七级 |园豆:41034 | 2019-05-10 10:05
其他回答(1)
0
var result = JsonConvert.DeserializeObject<Response>(Regex.Replace(result, "(\\+?\\d+)", "\"$1\"", RegexOptions.Multiline));

感谢Dudu大神提供的思路

supperAdministrator | 园豆:12 (初学一级) | 2019-05-10 10:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册