简体   繁体   中英

Consuming Varying JSON Response

I am having trouble understanding how to deserialze a JSON string into a class that's coming back from an API I am calling...

Normally, I copy the returned JSON and in Visual Studio I paste special as JSON to Class. It normally works perfectly.

But, with this API the class that is generated contains hardcoded values that might be different the next time I call the API...

This is the JSON that comes back from the API:

{"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":[]}}

This is the class that Visual Studio generates from the above 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; }
}

My problem is that if you look at the Tips class it contains values like _2360626 which might not be there next time.

How to I write a class that I can deserialize this string into that is "dynamic"? if that is the right description?

Thanks

I think you'll have to put aside this idea of creating a statically typed class in C# to model this JSON. JSON (and javascript) is not typed so there's not guarantee that it will always fit into such a class (as you've discovered).

I would suggest using one of the C# JSON parsing libraries you'll find referenced here: http://json.org/

JSON.Net ( http://james.newtonking.com/pages/json-net.aspx ) sounds popular.

Link that in your project and then use it to dynamically parse the JSON you're receiving from your API call and then do whatever you need to do with the result.

Just declare tips as 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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM