3

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?

Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
Jarvis Bot
  • 481
  • 6
  • 15
  • Avoid asking several questions in one, please. Just post them as a separated questions instead. http://meta.stackexchange.com/questions/212863/answering-multiple-questions-in-a-single-post – vbo Jan 22 '14 at 22:51
  • its not several questions. Looking for efficiency in this context with multiple choices. – Jarvis Bot Jan 22 '14 at 22:54
  • Maybe you just need to reform this a bit. For now it looks like this: 1) How to convert JSON string to object, that can be queried with LINQ? 2) How to store this object in db? 3) How to map it to something strongly typed? – vbo Jan 22 '14 at 22:56
  • @vbo you're correct. simplified now – Jarvis Bot Jan 22 '14 at 23:02
  • It's better. But it's 1) How to convert...? 2) Strong classes vs dynamic-like mapping. By the way second question is obvious. Classes are better in performance - dynamic things mean more code executed at runtime. But there is an overhead in development if you hardcode all the properties. – vbo Jan 22 '14 at 23:06

2 Answers2

5

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

Geezer68
  • 391
  • 2
  • 8
  • 1
    Geez. Will a more recent version of Visual Studio also brew coffee and make sandwiches? – Anton Tykhyy Jan 22 '14 at 23:05
  • @Geezer68 How will the data attributes look like? – Jarvis Bot Jan 22 '14 at 23:30
  • @AntonTykhyy You should see what they have planned for VS2014! (Especially [Refactoring](http://blogs.msdn.com/b/somasegar/archive/2014/05/28/first-preview-of-visual-studio-quot-14-quot-available-now.aspx)) – Dan Esparza Jul 31 '14 at 14:42
4

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

Community
  • 1
  • 1
vbo
  • 13,583
  • 1
  • 25
  • 33