Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient method to work with JSON

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?

like image 528
Jarvis Bot Avatar asked Sep 18 '26 05:09

Jarvis Bot


1 Answers

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

like image 152
Geezer68 Avatar answered Sep 20 '26 17:09

Geezer68