1

我无法理解如何将 JSON 字符串反序列化为从我正在调用的 API 返回的类...

通常,我复制返回的 JSON 并在 Visual Studio 中将特殊的 JSON 粘贴到 Class。它通常可以完美运行。

但是,使用这个 API,生成的类包含硬编码的值,下次我调用 API 时可能会有所不同......

这是从 API 返回的 JSON:

{"content":{"type":"getTips_answer","total":61,"scan":1,"tips":{"2360626":[["3","4","6","8","10","25"],["9","11","22","27","34","40"],["4","12","25","27","29","30"],["4","6","7","10","41","47"],["14","17","20","40","41","42"],["17","20","22","26","30","41"],["1","3","18","21","28","39"],["5","10","13","19","25","39"],["17","25","33","38","42","44"],["5","11","16","25","43","45"],["5","9","25","27","38","44"],["7","19","27","32","46","48"],["20","21","28","34","37","48"],["10","12","18","35","43","47"],["3","6","9","17","29","42"],["22","26","28","36","43","47"],["7","13","24","30","45","48"],["3","5","14","19","23","27"],["9","14","15","16","22","40"],["10","18","26","36","41","46"],["2","19","23","33","38","42"]],"2360710":[["1","15","17","21","25","33"]],"2361097":[["1","3","5","27","35","41"],["3","11","12","24","27","48"],["8","11","13","33","34","48"],["2","10","29","31","41","46"]],"2362535":[["10","15","33","35","36","44"],["5","8","11","18","26","44"]],"2363152":[["6","7","10","13","35","37"],["1","9","14","29","42","49"],["23","24","26","32","35","45"],["1","2","11","18","22","39"]],"2363573":[["8","11","16","18","34","36"],["12","13","23","25","27","35"],["2","7","13","23","41","49"],["3","6","9","15","21","41"],["9","10","16","20","30","34"],["15","18","40","44","46","48"]],"2363902":[["17","19","24","26","33","48"]],"2364026":[["8","17","20","33","34","47"]],"2364405":[["10","17","23","38","41","45"],["9","13","27","33","36","41"]],"2365222":[["4","5","7","9","18","24"],["10","12","16","26","43","45"],["1","5","24","26","43","47"],["11","12","17","20","36","48"],["3","11","13","17","20","27"],["2","6","28","38","42","46"],["9","13","19","20","25","31"],["2","5","7","8","25","27"],["1","19","21","23","33","36"],["17","19","23","33","38","47"],["14","27","28","32","39","42"],["18","26","30","32","42","46"]],"2365522":[["2","24","25","31","42","48"],["13","16","31","39","43","45"]],"2365651":[["7","20","22","35","40","41"],["5","13","20","30","43","47"],["5","16","18","31","34","44"],["6","8","15","17","44","45"],["7","11","26","27","29","47"]]},"success":true,"errors":[]}}

这是 Visual Studio 从上述 JSON 生成的类:

public class Rootobject
{
    public Content content { get; set; }
}

public class Content
{
    public string type { get; set; }
    public int total { get; set; }
    public int scan { get; set; }
    public Tips tips { get; set; }
    public bool success { get; set; }
    public object[] errors { get; set; }
}

public class Tips
{
    public string[][] _2360626 { get; set; }
    public string[][] _2360710 { get; set; }
    public string[][] _2361097 { get; set; }
    public string[][] _2362535 { get; set; }
    public string[][] _2363152 { get; set; }
    public string[][] _2363573 { get; set; }
    public string[][] _2363902 { get; set; }
    public string[][] _2364026 { get; set; }
    public string[][] _2364405 { get; set; }
    public string[][] _2365222 { get; set; }
    public string[][] _2365522 { get; set; }
    public string[][] _2365651 { get; set; }
}

我的问题是,如果您查看 Tips 类,它包含像 _2360626 这样的值,下次可能不会出现。

如何编写一个可以将此字符串反序列化为“动态”的类?如果这是正确的描述?

谢谢

4

2 回答 2

1

我认为您将不得不放弃在 C# 中创建静态类型类来建模此 JSON 的想法。JSON(和javascript)没有输入,所以不能保证它总是适合这样的类(正如你所发现的那样)。

我建议使用您会在此处找到引用的 C# JSON 解析库之一:http: //json.org/

JSON.Net ( http://james.newtonking.com/pages/json-net.aspx ) 听起来很流行。

将其链接到您的项目中,然后使用它来动态解析您从 API 调用接收到的 JSON,然后对结果执行您需要执行的任何操作。

于 2013-09-11T13:50:20.003 回答
1

只需声明tipsDictionary<string,List<List<string>>>

var obj = JsonConvert.DeserializeObject<RootObject>(json); //Json.Net
//or
//var obj = new JavaScriptSerializer().Deserialize<RootObject>(json);

public class Content
{
    public string type { get; set; }
    public int total { get; set; }
    public int scan { get; set; }
    public Dictionary<string,List<List<string>>> tips { get; set; }
    public bool success { get; set; }
    public List<object> errors { get; set; }
}

public class RootObject
{
    public Content content { get; set; }
}
于 2013-09-11T13:51:24.047 回答