我无法理解如何将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,下次可能不存在。
如何编写一个可以将此字符串反序列化为“动态”的类?如果这是正确的描述?
由于
答案 0 :(得分:1)
我认为你不得不放弃在C#中创建一个静态类型的类来构建这个JSON的想法。 JSON(和javascript)没有输入,所以不能保证它总是适合这样的类(如你所发现的那样)。
我建议使用您在此处引用的C#JSON解析库之一: http://json.org/
JSON.Net(http://james.newtonking.com/pages/json-net.aspx)听起来很受欢迎。
在项目中链接,然后使用它动态解析您从API调用中收到的JSON,然后执行您需要处理的结果。
答案 1 :(得分:1)
只需将tips
声明为Dictionary<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; }
}