合同或类的JSON?

时间:2016-02-23 18:08:23

标签: c# .net json visual-studio

是否有指南或程序可以采取示例 JSON 响应并生成序列化类或合同?

我无法确定如何从中等大小的JSON类型构建合同,但我确实有一个示例响应。

我的 json 响应包含子类型,例如

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

1 个答案:

答案 0 :(得分:4)

你可以使用http://json2csharp.com/参见我之前做过的例子:

我希望下面的例子会有所帮助。我总是设计一个与json相匹配的模型。当它是您自己的模型设计时,使用该对象要好得多。

从json生成c#模型非常容易。我使用此网站生成模型:http://json2csharp.com

一个完整的例子是:

C#代码:

   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"
        }
      ]
    }
  ]
}

使用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; }
}

我希望这个完整的例子可以帮到你。祝你有个美好的一天!