Efficient method to work with JSON

164 Views Asked by At

I receive this JSON response:

{
    "status": "Ok",
    "total": 105373,
    "result": [
        {
            "id": 668839,
            "cn": "A-12157-68812",
            "pD": "09-12-2012",
            "cCD": "06-05-2012",
            "cT": "PERM",
            "fN": "COMCAST CABLE COMMUNICATIONS, LLC",
            "s": "NJ",
            "cR": "Certified",
            "pT": "ENGINEER 3",
            "vC": 6,
            "cddt": "html"
        },
        {
            "id": 725695,
            "cn": "A-12361-25668",
            "pD": "05-28-2013",
            "cCD": "12-26-2012",
            "cT": "PERM",
            "fN": "L'ONVIE INC.",
            "s": "NY",
            "cR": "Certified",
            "pT": "Biochemist",
            "vC": 6,
            "cddt": "html"
        },
        {
            "id": 0,
            "cn": "A-11199-93239",
            "pD": "05-31-2012",
            "cCD": "07-18-2011",
            "cT": "PERM",
            "fN": "ROCKWELL AUTOMATION, INC.",
            "s": "PR",
            "cR": "Certified",
            "pT": "Marketing Managers",
            "vC": -1
        }]
}
  1. What is the best way to convert this into an object so that I can apply various LINQ transforms to query for some meaningful data?

What is the efficient way to do this?

Should I create classes like:

public class Status {
                        string Status {get; set;}
                        int Total {get; set;}
                        IEnumerable<Result> Results {get; set;}
                        }



public class Result {
                    string Id {get; set;}
                    ..................
                   }

And then figure out mapping/casting?

2

There are 2 best solutions below

0
On

Try Json.NET. Check out this docs about LINQ queries. You can even use Json.NET to map your JSON to .NET objects automatically.

3
On

If you are using a recent version of Visual Studio you can use Edit > Paste Special > Paste JSON as Classes. This will save you loads of time:)

You will need to create your own classes if you are doing anything more than simple operations on the data. You can rename the fields from Json to more suitable names by using data attributes.

It depends on which deserializer you use though - Json.net or a built in .net version.

Try searching for dataContract and dataMember if you use a built-in .net deserializer or jsonProperty if you use json.net