Reputation: 133
Is there a guide or program that can take a sample JSON response and generate a serializeable class or contract?
I am having trouble figuring out how to build the contract from a moderately sized JSON type but I do have a sample response.
My json response has sub types such as
{
"metadata" : {
"queryTime" : "450 ms",
"timeStamp" : "2016-02-23 13:56:47 UTC",
"message" : "",
"requestData" : {
"apiKey" : OMITTED,
"part" : "LM741",
"limit" : 15,
"exactMatch" : false,
"softWaitTime" : 1000,
"hardWaitTime" : 2500,
"useHardWait" : false,
"hostedOnly" : false,
"authorizedOnly" : true
}
},
"response" : [ {
"distributor" : {
"id" : 2167609,
"name" : "Verical",
"authorized" : true,
"logoUrl" : "https://media.findchips.com/srp/logos/results/2167609_fc.gif"
},
"parts" : [ {
"manufacturer" : "Texas Instruments",
"part" : "LM741J",
"description" : "OP Amp Single GP ��22V 8-Pin CDIP Rail",
"price" : [ {
"quantity" : 4,
"price" : 9.049,
"currency" : "USD"
}, {
"quantity" : 10,
"price" : 8.229,
"currency" : "USD"
}, {
"quantity" : 25,
"price" : 7.406,
"currency" : "USD"
"response" : [ {
"distributor" : {
"id" : 2167609,
"name" : "Verical",
"authorized" : true,
"logoUrl" : "https://media.findchips.com/srp/logos/results/2167609_fc.gif"
},
"parts" : [ {
"manufacturer" : "Texas Instruments",
"part" : "LM741J",
"description" : "OP Amp Single GP ��22V 8-Pin CDIP Rail",
"price" : [ {
"quantity" : 4,
"price" : 9.049,
"currency" : "USD"
}, {
"quantity" : 10,
"price" : 8.229,
"currency" : "USD"
}, {
"quantity" : 25,
"price" : 7.406,
"currency" : "USD"
}, {
"quantity" : 100,
"price" : 6.85,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=7rc66yc&p=LM741J&s=LM741&h=EMADK_lvA9SeYqOzjTMmIg",
"stock" : 146,
"rohs" : {
"DEFAULT" : "Not compliant"
},
"lastUpdated" : "2016-02-23 13:56:47 UTC"
}, {
"manufacturer" : "Texas Instruments",
"part" : "LM741J",
"description" : "OP Amp Single GP ��22V 8-Pin CDIP Rail",
"price" : [ {
"quantity" : 0,
"price" : 7.2914,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=7rc66yc&p=LM741J&s=LM741&h=EMADK_lvA9SeYqOzjTMmIg",
"stock" : 10000,
"rohs" : {
"DEFAULT" : "Not compliant"
},
"lastUpdated" : "2016-02-23 13:56:47 UTC"
}, {
"manufacturer" : "Texas Instruments",
"part" : "LM741J/883",
"description" : "OP Amp Single GP ±22V 8-Pin CDIP Rail",
"price" : [ {
"quantity" : 0,
"price" : 11.8,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=7rc66yc&p=LM741J%2F883&s=LM741&h=r3wmym0oQUfe4WH5DQ_TkQ",
"stock" : 516,
"rohs" : {
"DEFAULT" : "Not compliant"
},
"lastUpdated" : "2016-02-23 13:56:47 UTC"
}, {
"manufacturer" : "Texas Instruments",
"part" : "LM741H/NOPB",
"description" : "OP Amp Single GP ±22V 8-Pin TO-99 Box",
"price" : [ {
"quantity" : 5,
"price" : 6.566,
"currency" : "USD"
}, {
"quantity" : 10,
"price" : 5.885,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=7rc66yc&p=LM741H%2FNOPB&s=LM741&h=KiNzrWZqMH6yOB9upLNCJg",
"stock" : 499,
"rohs" : {
"DEFAULT" : "Compliant"
},
"lastUpdated" : "2016-02-23 13:56:47 UTC"
}, {
"manufacturer" : "Texas Instruments",
"part" : "LM741H/NOPB",
"description" : "OP Amp Single GP ±22V 8-Pin TO-99 Box",
"price" : [ {
"quantity" : 0,
"price" : 5.2829,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=7rc66yc&p=LM741H%2FNOPB&s=LM741&h=KiNzrWZqMH6yOB9upLNCJg",
"stock" : 3000,
"rohs" : {
"DEFAULT" : "Compliant"
},
"lastUpdated" : "2016-02-23 13:56:47 UTC"
}, {
"manufacturer" : "Texas Instruments",
"part" : "LM741CHNOPB",
"description" : "OP Amp Single GP ±18V 8-Pin TO-99 Box",
"price" : [ {
"quantity" : 5,
"price" : 6.949,
"currency" : "USD"
}, {
"quantity" : 10,
"price" : 6.106,
"currency" : "USD"
}, {
"quantity" : 100,
"price" : 5.286,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=7rc66yc&p=LM741CHNOPB&s=LM741&h=Kwc2_lrFk5O25e_0nqq_7w",
"stock" : 457,
"rohs" : {
"DEFAULT" : "Compliant"
},
"lastUpdated" : "2016-02-23 13:56:47 UTC"
}, {
"manufacturer" : "Texas Instruments",
"part" : "LM741CHNOPB",
"description" : "OP Amp Single GP ±18V 8-Pin TO-99 Box",
"price" : [ {
"quantity" : 0,
"price" : 5.2829,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=7rc66yc&p=LM741CHNOPB&s=LM741&h=Kwc2_lrFk5O25e_0nqq_7w",
"stock" : 10000,
"rohs" : {
"DEFAULT" : "Compliant"
},
"lastUpdated" : "2016-02-23 13:56:47 UTC"
}, {
"manufacturer" : "Texas Instruments",
"part" : "LM741H/883",
"description" : "OP Amp Single GP ±22V 8-Pin TO-99 Tray",
"price" : [ {
"quantity" : 0,
"price" : 15.74,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=7rc66yc&p=LM741H%2F883&s=LM741&h=wth-f1s8kx9k7KPFT6SaDg",
"stock" : 943,
"rohs" : {
"DEFAULT" : "Not compliant"
},
"lastUpdated" : "2016-02-23 13:56:47 UTC"
}, {
"manufacturer" : "Texas Instruments",
"part" : "LM741H",
"description" : "OP Amp Single GP ±22V 8-Pin TO-99 Box",
"price" : [ {
"quantity" : 5,
"price" : 6.948,
"currency" : "USD"
}, {
"quantity" : 250,
"price" : 5.121,
"currency" : "USD"
}, {
"quantity" : 500,
"price" : 4.707,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=7rc66yc&p=LM741H&s=LM741&h=Yna7ea4rAH2j0wA_r-Hfyw",
"stock" : 522,
"rohs" : {
"DEFAULT" : "Not compliant"
},
"lastUpdated" : "2016-02-23 13:56:47 UTC"
}, {
"manufacturer" : "Texas Instruments",
"part" : "LM741H",
"description" : "OP Amp Single GP ±22V 8-Pin TO-99 Box",
"price" : [ {
"quantity" : 0,
"price" : 5.2829,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=7rc66yc&p=LM741H&s=LM741&h=Yna7ea4rAH2j0wA_r-Hfyw",
"stock" : 4000,
"rohs" : {
"DEFAULT" : "Not compliant"
},
"lastUpdated" : "2016-02-23 13:56:47 UTC"
}, {
"manufacturer" : "Texas Instruments",
"part" : "LM741CN/NOPB",
"description" : "OP Amp Single GP ±18V 8-Pin PDIP Tube",
"price" : [ {
"quantity" : 0,
"price" : 0.25,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=7rc66yc&p=LM741CN%2FNOPB&s=LM741&h=Ulrpi_7vidcH37VKgv3AJg",
"stock" : 10000,
"rohs" : {
"DEFAULT" : "Compliant"
},
"lastUpdated" : "2016-02-23 13:56:47 UTC"
}, {
"manufacturer" : "Texas Instruments",
"part" : "LM741CN/NOPB",
"description" : "OP Amp Single GP ±18V 8-Pin PDIP Tube",
"price" : [ {
"quantity" : 120,
"price" : 0.3777,
"currency" : "USD"
}, {
"quantity" : 1000,
"price" : 0.2475,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=7rc66yc&p=LM741CN%2FNOPB&s=LM741&h=Ulrpi_7vidcH37VKgv3AJg",
"stock" : 2560,
"rohs" : {
"DEFAULT" : "Compliant"
},
"lastUpdated" : "2016-02-23 13:56:47 UTC"
}, {
"manufacturer" : "Texas Instruments",
"part" : "LM741CH",
"description" : "OP Amp Single GP ±18V 8-Pin TO-99 Box",
"price" : [ {
"quantity" : 0,
"price" : 5.2829,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=7rc66yc&p=LM741CH&s=LM741&h=ciug0AlmoXxX8xmu5zyU2w",
"stock" : 2000,
"rohs" : {
"DEFAULT" : "Not compliant"
},
"lastUpdated" : "2016-02-23 13:56:47 UTC"
}, {
"manufacturer" : "Texas Instruments",
"part" : "LM741CH",
"description" : "OP Amp Single GP ±18V 8-Pin TO-99 Box",
"price" : [ {
"quantity" : 5,
"price" : 6.872,
"currency" : "USD"
}, {
"quantity" : 10,
"price" : 6.186,
"currency" : "USD"
}, {
"quantity" : 100,
"price" : 5.356,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=7rc66yc&p=LM741CH&s=LM741&h=ciug0AlmoXxX8xmu5zyU2w",
"stock" : 464,
"rohs" : {
"DEFAULT" : "Not compliant"
},
"lastUpdated" : "2016-02-23 13:56:47 UTC"
} ]
}, {
"distributor" : {
"id" : 1588,
"name" : "Digi-Key",
"authorized" : true,
"logoUrl" : "https://media.findchips.com/srp/logos/results/1588_fc.gif"
},
"parts" : [ {
"manufacturer" : "Texas Instruments",
"part" : "LM741J",
"distributorItemNo" : "296-39028-5-ND",
"description" : "IC OPERATIONAL AMPLIFIER 8CDIP",
"price" : [ {
"quantity" : 5000,
"price" : 6.38,
"currency" : "USD"
}, {
"quantity" : 2500,
"price" : 6.53312,
"currency" : "USD"
}, {
"quantity" : 1000,
"price" : 6.78832,
"currency" : "USD"
}, {
"quantity" : 500,
"price" : 7.4008,
"currency" : "USD"
}, {
"quantity" : 100,
"price" : 8.3195,
"currency" : "USD"
}, {
"quantity" : 10,
"price" : 9.851,
"currency" : "USD"
}, {
"quantity" : 1,
"price" : 10.72,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=772rcyc&p=LM741J&s=LM741&h=suDVObn-FXUNhmpOUFuGgg",
"stock" : 30,
"lastUpdated" : "2016-02-23 13:00:00 UTC"
}, {
"manufacturer" : "Texas Instruments",
"part" : "LM741H",
"distributorItemNo" : "296-39027-ND",
"description" : "IC OPAMP GP 1.5MHZ TO99-8",
"price" : [ {
"quantity" : 5000,
"price" : 4.6225,
"currency" : "USD"
}, {
"quantity" : 2500,
"price" : 4.8074,
"currency" : "USD"
}, {
"quantity" : 1000,
"price" : 4.9923,
"currency" : "USD"
}, {
"quantity" : 500,
"price" : 5.7319,
"currency" : "USD"
}, {
"quantity" : 100,
"price" : 6.5824,
"currency" : "USD"
}, {
"quantity" : 10,
"price" : 7.951,
"currency" : "USD"
}, {
"quantity" : 1,
"price" : 8.8,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=772rcyc&p=LM741H&s=LM741&h=CtVHlTPTE47y5mY65vg-ow",
"stock" : 405,
"lastUpdated" : "2016-02-23 13:00:00 UTC"
}, {
"manufacturer" : "Texas Instruments",
"part" : "LM741CH",
"distributorItemNo" : "296-43454-ND",
"description" : "IC OPAMP GP 1.5MHZ TO99-8",
"price" : [ {
"quantity" : 5000,
"price" : 4.6225,
"currency" : "USD"
}, {
"quantity" : 2500,
"price" : 4.8074,
"currency" : "USD"
}, {
"quantity" : 1000,
"price" : 4.9923,
"currency" : "USD"
}, {
"quantity" : 500,
"price" : 5.7319,
"currency" : "USD"
}, {
"quantity" : 100,
"price" : 6.5824,
"currency" : "USD"
}, {
"quantity" : 10,
"price" : 7.951,
"currency" : "USD"
}, {
"quantity" : 1,
"price" : 8.8,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=772rcyc&p=LM741CH&s=LM741&h=-Wqwmv7O_eTVbX_CUamFSQ",
"stock" : 500,
"lastUpdated" : "2016-02-23 13:00:00 UTC"
}, {
"manufacturer" : "Texas Instruments",
"part" : "LM741H/NOPB",
"distributorItemNo" : "LM741H/NOPB-ND",
"description" : "IC OPAMP GP 1.5MHZ TO99-8",
"price" : [ {
"quantity" : 5000,
"price" : 4.6225,
"currency" : "USD"
}, {
"quantity" : 2500,
"price" : 4.8074,
"currency" : "USD"
}, {
"quantity" : 1000,
"price" : 4.9923,
"currency" : "USD"
}, {
"quantity" : 500,
"price" : 5.7319,
"currency" : "USD"
}, {
"quantity" : 100,
"price" : 6.5824,
"currency" : "USD"
}, {
"quantity" : 10,
"price" : 7.951,
"currency" : "USD"
}, {
"quantity" : 1,
"price" : 8.8,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=772rcyc&p=LM741H%2FNOPB&s=LM741&h=QYuEbe1c-H7muy8z4f1l3A",
"stock" : 384,
"lastUpdated" : "2016-02-23 13:00:00 UTC"
}, {
"manufacturer" : "Texas Instruments",
"part" : "LM741CH/NOPB",
"distributorItemNo" : "LM741CH/NOPB-ND",
"description" : "IC OPAMP GP 1.5MHZ TO99-8",
"price" : [ {
"quantity" : 5000,
"price" : 4.6225,
"currency" : "USD"
}, {
"quantity" : 2500,
"price" : 4.8074,
"currency" : "USD"
}, {
"quantity" : 1000,
"price" : 4.9923,
"currency" : "USD"
}, {
"quantity" : 500,
"price" : 5.7319,
"currency" : "USD"
}, {
"quantity" : 100,
"price" : 6.5824,
"currency" : "USD"
}, {
"quantity" : 10,
"price" : 7.951,
"currency" : "USD"
}, {
"quantity" : 1,
"price" : 8.8,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=772rcyc&p=LM741CH%2FNOPB&s=LM741&h=OXSewhvN77ZwbR_OjuvTWQ",
"stock" : 1196,
"lastUpdated" : "2016-02-23 13:00:00 UTC"
}, {
"manufacturer" : "Texas Instruments",
"part" : "LM741CN/NOPB",
"distributorItemNo" : "LM741CNNS/NOPB-ND",
"description" : "IC OPAMP GP 1.5MHZ 8DIP",
"price" : [ {
"quantity" : 10000,
"price" : 0.2275,
"currency" : "USD"
}, {
"quantity" : 5000,
"price" : 0.23625,
"currency" : "USD"
}, {
"quantity" : 2500,
"price" : 0.25375,
"currency" : "USD"
}, {
"quantity" : 1000,
"price" : 0.28,
"currency" : "USD"
}, {
"quantity" : 500,
"price" : 0.35,
"currency" : "USD"
}, {
"quantity" : 100,
"price" : 0.4428,
"currency" : "USD"
}, {
"quantity" : 10,
"price" : 0.578,
"currency" : "USD"
}, {
"quantity" : 1,
"price" : 0.66,
"currency" : "USD"
} ],
"buyNowUrl" : "http://api.supplyframe.com/v1/t?d=772rcyc&p=LM741CN%2FNOPB&s=LM741&h=Hz7l-1YgZdf7hUceKcjMyg",
"stock" : 19573,
"lastUpdated" : "2016-02-23 13:00:00 UTC"
} ]
} ]
}
Upvotes: 1
Views: 197
Reputation: 444
you can use http://json2csharp.com/ See the example I did before:
I hope the below example will help. I always design a model that match the json. It is much better to work with the object when it is your own model design.
It is very easy to generate the c# model from the json. I use this website to generate the model: http://json2csharp.com
A complete example is:
C# Code:
var targetsObject = Newtonsoft.Json.JsonConvert.DeserializeObject<YourModel>(jsonString);
JSON:
{
"investors": [
{
"name": "06",
"programs": [
{
"name": "Conventional",
"value": "3.5"
},
{
"name": "FHA - Standard",
"value": "5.0"
},
{
"name": "FHA - Streamline",
"value": ""
},
{
"name": "VA",
"value": "5.5"
},
{
"name": "VA IRRRL",
"value": "6.0"
},
{
"name": "Non-Prime",
"value": ""
}
]
},
{
"name": "07",
"programs": [
{
"name": "Conventional",
"value": "3.5"
},
{
"name": "FHA - Standard",
"value": "5.0"
},
{
"name": "FHA - Streamline",
"value": "7.0"
},
{
"name": "VA",
"value": "5.5"
},
{
"name": "VA IRRRL",
"value": ""
},
{
"name": "Non-Prime",
"value": ""
}
]
},
{
"name": "08",
"programs": [
{
"name": "Conventional",
"value": "3.5"
},
{
"name": "FHA - Standard",
"value": "5.0"
},
{
"name": "FHA - Streamline",
"value": "7.0"
},
{
"name": "VA",
"value": "5.5"
},
{
"name": "VA IRRRL",
"value": ""
},
{
"name": "Non-Prime",
"value": ""
}
]
},
{
"name": "09",
"programs": [
{
"name": "Conventional",
"value": "3.5"
},
{
"name": "FHA - Standard",
"value": "5.0"
},
{
"name": "FHA - Streamline",
"value": ""
},
{
"name": "VA",
"value": "5.5"
},
{
"name": "VA IRRRL",
"value": ""
},
{
"name": "Non-Prime",
"value": ""
}
]
},
{
"name": "10",
"programs": [
{
"name": "Conventional",
"value": ""
},
{
"name": "FHA - Standard",
"value": ""
},
{
"name": "FHA - Streamline",
"value": ""
},
{
"name": "VA",
"value": ""
},
{
"name": "VA IRRRL",
"value": ""
},
{
"name": "Non-Prime",
"value": "2.0"
}
]
},
{
"name": "11",
"programs": [
{
"name": "Conventional",
"value": "3.5"
},
{
"name": "FHA - Standard",
"value": "5.0"
},
{
"name": "FHA - Streamline",
"value": ""
},
{
"name": "VA",
"value": "6.0"
},
{
"name": "VA IRRRL",
"value": "6.0"
},
{
"name": "Non-Prime",
"value": ""
}
]
},
{
"name": "12",
"programs": [
{
"name": "Conventional",
"value": "3.5"
},
{
"name": "FHA - Standard",
"value": "5.0"
},
{
"name": "FHA - Streamline",
"value": ""
},
{
"name": "VA",
"value": "5.5"
},
{
"name": "VA IRRRL",
"value": "6.0"
},
{
"name": "Non-Prime",
"value": ""
}
]
},
{
"name": "13",
"programs": [
{
"name": "Conventional",
"value": ""
},
{
"name": "FHA - Standard",
"value": "5.0"
},
{
"name": "FHA - Streamline",
"value": ""
},
{
"name": "VA",
"value": ""
},
{
"name": "VA IRRRL",
"value": ""
},
{
"name": "Non-Prime",
"value": "2.0"
}
]
}
]
}
Model Generated automatically using http://json2csharp.com/
public class Program
{
public string name { get; set; }
public string value { get; set; }
}
public class Investor
{
public string name { get; set; }
public List<Program> programs { get; set; }
}
public class RootObject
{
public List<Investor> investors { get; set; }
}
I hope this complete example helps you. Have a good day!
Upvotes: 4