将JSON转换为C#内联类,并设置值

时间:2015-12-07 11:54:10

标签: c# json unit-testing

我需要从设置了值的JSON对象生成测试用例(使用IEnumerable<TestCaseData>)。我找不到任何可以生成具有值集(Plenty that generate classes)的C#类的在线工具。在测试用例中反序列化Json会使测试无效(One应该只测试代码),so I did this regex

当所有属性都是相同类型(小数或bools)时,它并不完美,但对于大多数基本格式良好的JSON工作得很好, 模式:"([a-zA-Z]?[a-zA-Z0-9].*)" ?: ?((true|false|null)|([\d].*)),替换$1 = $2M但是当有很多类型并且它们混合时我会被搞砸。

我确信有人在我之前碰到了这个,所以我正在重新发明轮子。简而言之。

我该怎么做:

{
    "LegalFeeNet": 363.54,
    "LegalFeeVat": 72.708,
    "DiscountNet": 0.0,
    "DiscountVat": 0.0,
    "OtherNet": 12.0,
    "OtherVat": 2.4,
    "DisbursementNet": 220.0,
    "DisbursementVat": 0.0,
    "AmlCheck": null,
    "LegalSubTotal": 363.54,
    "TotalFee": 450.648,
    "Discounts": 0.0,
    "Vat": 75.108,
    "DiscountedPrice": 360.5184,
    "RecommendedRetailPrice": 450.648,
    "SubTotal": 375.54,
    "Name": "Will",
    "IsDiscounted": false,
    "CustomerCount": 3
}

成为这个:

ClassName {
    LegalFeeNet = 363.54M,
    LegalFeeVat = 72.708M,
    DiscountNet = 0.0M,
    DiscountVat = 0.0M,
    OtherNet = 12.0M,
    OtherVat = 2.4M,
    DisbursementNet = 220.0M,
    DisbursementVat = 0.0M,
    AmlCheck = nullM,
    LegalSubTotal = 363.54M,
    TotalFee = 450.648M,
    Discounts = 0.0M,
    Vat = 75.108M,
    DiscountedPrice = 360.5184M,
    RecommendedRetailPrice = 450.648M,
    SubTotal = 375.54M,
    Name = "Will",
    IsDiscounted = false,
    CustomerCount = 3
}

生成具有从JSON对象设置的属性的C#类,最快/最方便的解决方案是什么?

2 个答案:

答案 0 :(得分:4)

我也在这里寻找相同问题的解决方案。

被接受的答案缺少了我想要的一些功能,因此最终创建了这个功能 https://jsontocsharpconverter.web.app/

希望..它可以帮助某人。

答案 1 :(得分:2)

所以我没有找到任何开箱即用的解决方案 - 必须自己编写。

下面的脚本可以用作转换器,它可能充满了bug。它仍然适用于我迄今为止所需要的一切。

&#13;
&#13;
function Convert(jsonStr, classNr) {
  var i = classNr == undefined ? 0 : classNr;
  var str = "";
  var json = JSON.parse(jsonStr);
  for (var prop in json) {
    if (typeof(json[prop]) === "number") {
      if (json[prop] === +json[prop] && json[prop] !== (json[prop] | 0)) {
        str += prop + " = " + json[prop] + "M, ";
      } else {
        str += prop + " = " + json[prop] + ", ";
      }
    } else if (typeof(json[prop]) === "boolean") {
      str += prop + " = " + json[prop] + ", ";
    } else if (typeof(json[prop]) === "string") {
      str += prop + ' = "' + json[prop] + '", ';
    } else if (json[prop] == null || json[prop] == undefined) {
      str += prop + ' = null, ';
    } else if (typeof(json[prop]) === "object") {
      str += prop + " = " + Convert(JSON.stringify(json[prop]), i++) + ", ";
    }
  }
  return "new Class" + i + "{ " + str + " }";
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<textarea cols="100" rows="6">
  { "StingProperty" : "StringVal", "LegalFeeNet": 363.54, "LegalFeeVat": 72.708, "DiscountNet": 0.0, "DiscountVat": 0.0, "OtherNet": 12.0, "OtherVat": 2.4, "DisbursementNet": 220.0, "DisbursementVat": 0.0, "AmlCheck": null, "LegalSubTotal": 363.54, "TotalFee":
  450.648, "Discounts": 0.0, "Vat": 75.108, "DiscountedPrice": 360.5184, "RecommendedRetailPrice": 450.648, "SubTotal": 375.54, "Name": "Will", "IsDiscounted": false, "CustomerCount": 3, "Obj" : {"One" : 1, "Dec" : 1.1, "Str" : "Stringer", "Bolie" : true},"Obj1"
  : {"One" : 1, "Dec" : 1.1, "Str" : "Stringer", "Bolie" : true, "Obj2" : {"One" : 1, "Dec" : 1.1, "Str" : "Stringer", "Bolie" : true}} }
</textarea>
<input type="button" value="Just do it!" onclick="$('#result').append(Convert($('textarea').text()));" />
<div id="result"></div>
&#13;
&#13;
&#13;