HTML JSON字符串(在Ajax调用之前)=>

"{
 "UnitOfMeasureRelatedUnitDataInbound": [
  {
   "Name": "test",
   "Active": true,
   "UnitOfMeasureTypeID": "dd89f0a0-59c3-49a1-a2ae-7e763da32065",
   "BaseUnitID": "4c835ebb-60f2-435f-a5f4-8dc311fbbca0",
   "BaseUnitName": null,
   "BaseUnitAbbreviation": null,
   "RelatedUnitDisplayOrder": 1,
   "RelatedUnitName": "Inch",
   "RelatedUnitAbbreviation": "in",
   "RelatedUnitConversionRatio": 25.4,
   "UnitOfMeasureSetID": "69f5a1f5-b26c-5ef6-42ca-08ec6fc8ba64"
  },
  {
   "Name": "test",
   "Active": true,
   "UnitOfMeasureTypeID": "dd89f0a0-59c3-49a1-a2ae-7e763da32065",
   "BaseUnitID": "4c835ebb-60f2-435f-a5f4-8dc311fbbca0",
   "BaseUnitName": null,
   "BaseUnitAbbreviation": null,
   "RelatedUnitDisplayOrder": 2,
   "RelatedUnitName": "Foot",
   "RelatedUnitAbbreviation": "ft",
   "RelatedUnitConversionRatio": 304.8,
   "UnitOfMeasureSetID": "69f5a1f5-b26c-5ef6-42ca-08ec6fc8ba64"
  },
  {
   "Name": "test",
   "Active": true,
   "UnitOfMeasureTypeID": "dd89f0a0-59c3-49a1-a2ae-7e763da32065",
   "BaseUnitID": "4c835ebb-60f2-435f-a5f4-8dc311fbbca0",
   "BaseUnitName": null,
   "BaseUnitAbbreviation": null,
   "RelatedUnitDisplayOrder": 3,
   "RelatedUnitName": "Yard",
   "RelatedUnitAbbreviation": "yd",
   "RelatedUnitConversionRatio": 914.4,
   "UnitOfMeasureSetID": "69f5a1f5-b26c-5ef6-42ca-08ec6fc8ba64"
  },
  {
   "Name": "test",
   "Active": true,
   "UnitOfMeasureTypeID": "dd89f0a0-59c3-49a1-a2ae-7e763da32065",
   "BaseUnitID": "4c835ebb-60f2-435f-a5f4-8dc311fbbca0",
   "BaseUnitName": null,
   "BaseUnitAbbreviation": null,
   "RelatedUnitDisplayOrder": 4,
   "RelatedUnitName": "Centimeter",
   "RelatedUnitAbbreviation": "cm",
   "RelatedUnitConversionRatio": 10,
   "UnitOfMeasureSetID": "69f5a1f5-b26c-5ef6-42ca-08ec6fc8ba64"
  },
  {
   "Name": "test",
   "Active": true,
   "UnitOfMeasureTypeID": "dd89f0a0-59c3-49a1-a2ae-7e763da32065",
   "BaseUnitID": "4c835ebb-60f2-435f-a5f4-8dc311fbbca0",
   "BaseUnitName": null,
   "BaseUnitAbbreviation": null,
   "RelatedUnitDisplayOrder": 5,
   "RelatedUnitName": "Decimeter",
   "RelatedUnitAbbreviation": "dm",
   "RelatedUnitConversionRatio": 100,
   "UnitOfMeasureSetID": "69f5a1f5-b26c-5ef6-42ca-08ec6fc8ba64"
  },
  {
   "Name": "test",
   "Active": true,
   "UnitOfMeasureTypeID": "dd89f0a0-59c3-49a1-a2ae-7e763da32065",
   "BaseUnitID": "4c835ebb-60f2-435f-a5f4-8dc311fbbca0",
   "BaseUnitName": null,
   "BaseUnitAbbreviation": null,
   "RelatedUnitDisplayOrder": 6,
   "RelatedUnitName": "Meter",
   "RelatedUnitAbbreviation": "m",
   "RelatedUnitConversionRatio": 1000,
   "UnitOfMeasureSetID": "69f5a1f5-b26c-5ef6-42ca-08ec6fc8ba64"
  }
 ]
}"


var jsonString = JsonConvert.SerializeObject(UnitOfMeasureRelatedUnitDataInbound);

(在Ajax调用之后)=>

"{\"{\\n \\\"UnitOfMeasureRelatedUnitDataInbound\\\": \":{\"\\n  {\\n   \\\"Name\\\": \\\"test\\\",\\n   \\\"Active\\\": true,\\n   \\\"UnitOfMeasureTypeID\\\": \\\"dd89f0a0-59c3-49a1-a2ae-7e763da32065\\\",\\n   \\\"BaseUnitID\\\": \\\"4c835ebb-60f2-435f-a5f4-8dc311fbbca0\\\",\\n   \\\"BaseUnitName\\\": null,\\n   \\\"BaseUnitAbbreviation\\\": null,\\n   \\\"RelatedUnitDisplayOrder\\\": 1,\\n   \\\"RelatedUnitName\\\": \\\"Inch\\\",\\n   \\\"RelatedUnitAbbreviation\\\": \\\"in\\\",\\n   \\\"RelatedUnitConversionRatio\\\": 25.4,\\n   \\\"UnitOfMeasureSetID\\\": \\\"69f5a1f5-b26c-5ef6-42ca-08ec6fc8ba64\\\"\\n  },\\n  {\\n   \\\"Name\\\": \\\"test\\\",\\n   \\\"Active\\\": true,\\n   \\\"UnitOfMeasureTypeID\\\": \\\"dd89f0a0-59c3-49a1-a2ae-7e763da32065\\\",\\n   \\\"BaseUnitID\\\": \\\"4c835ebb-60f2-435f-a5f4-8dc311fbbca0\\\",\\n   \\\"BaseUnitName\\\": null,\\n   \\\"BaseUnitAbbreviation\\\": null,\\n   \\\"RelatedUnitDisplayOrder\\\": 2,\\n   \\\"RelatedUnitName\\\": \\\"Foot\\\",\\n   \\\"RelatedUnitAbbreviation\\\": \\\"ft\\\",\\n   \\\"RelatedUnitConversionRatio\\\": 304.8,\\n   \\\"UnitOfMeasureSetID\\\": \\\"69f5a1f5-b26c-5ef6-42ca-08ec6fc8ba64\\\"\\n  },\\n  {\\n   \\\"Name\\\": \\\"test\\\",\\n   \\\"Active\\\": true,\\n   \\\"UnitOfMeasureTypeID\\\": \\\"dd89f0a0-59c3-49a1-a2ae-7e763da32065\\\",\\n   \\\"BaseUnitID\\\": \\\"4c835ebb-60f2-435f-a5f4-8dc311fbbca0\\\",\\n   \\\"BaseUnitName\\\": null,\\n   \\\"BaseUnitAbbreviation\\\": null,\\n   \\\"RelatedUnitDisplayOrder\\\": 3,\\n   \\\"RelatedUnitName\\\": \\\"Yard\\\",\\n   \\\"RelatedUnitAbbreviation\\\": \\\"yd\\\",\\n   \\\"RelatedUnitConversionRatio\\\": 914.4,\\n   \\\"UnitOfMeasureSetID\\\": \\\"69f5a1f5-b26c-5ef6-42ca-08ec6fc8ba64\\\"\\n  },\\n  {\\n   \\\"Name\\\": \\\"test\\\",\\n   \\\"Active\\\": true,\\n   \\\"UnitOfMeasureTypeID\\\": \\\"dd89f0a0-59c3-49a1-a2ae-7e763da32065\\\",\\n   \\\"BaseUnitID\\\": \\\"4c835ebb-60f2-435f-a5f4-8dc311fbbca0\\\",\\n   \\\"BaseUnitName\\\": null,\\n   \\\"BaseUnitAbbreviation\\\": null,\\n   \\\"RelatedUnitDisplayOrder\\\": 4,\\n   \\\"RelatedUnitName\\\": \\\"Centimeter\\\",\\n   \\\"RelatedUnitAbbreviation\\\": \\\"cm\\\",\\n   \\\"RelatedUnitConversionRatio\\\": 10,\\n   \\\"UnitOfMeasureSetID\\\": \\\"69f5a1f5-b26c-5ef6-42ca-08ec6fc8ba64\\\"\\n  },\\n  {\\n   \\\"Name\\\": \\\"test\\\",\\n   \\\"Active\\\": true,\\n   \\\"UnitOfMeasureTypeID\\\": \\\"dd89f0a0-59c3-49a1-a2ae-7e763da32065\\\",\\n   \\\"BaseUnitID\\\": \\\"4c835ebb-60f2-435f-a5f4-8dc311fbbca0\\\",\\n   \\\"BaseUnitName\\\": null,\\n   \\\"BaseUnitAbbreviation\\\": null,\\n   \\\"RelatedUnitDisplayOrder\\\": 5,\\n   \\\"RelatedUnitName\\\": \\\"Decimeter\\\",\\n   \\\"RelatedUnitAbbreviation\\\": \\\"dm\\\",\\n   \\\"RelatedUnitConversionRatio\\\": 100,\\n   \\\"UnitOfMeasureSetID\\\": \\\"69f5a1f5-b26c-5ef6-42ca-08ec6fc8ba64\\\"\\n  },\\n  {\\n   \\\"Name\\\": \\\"test\\\",\\n   \\\"Active\\\": true,\\n   \\\"UnitOfMeasureTypeID\\\": \\\"dd89f0a0-59c3-49a1-a2ae-7e763da32065\\\",\\n   \\\"BaseUnitID\\\": \\\"4c835ebb-60f2-435f-a5f4-8dc311fbbca0\\\",\\n   \\\"BaseUnitName\\\": null,\\n   \\\"BaseUnitAbbreviation\\\": null,\\n   \\\"RelatedUnitDisplayOrder\\\": 6,\\n   \\\"RelatedUnitName\\\": \\\"Meter\\\",\\n   \\\"RelatedUnitAbbreviation\\\": \\\"m\\\",\\n   \\\"RelatedUnitConversionRatio\\\": 1000,\\n   \\\"UnitOfMeasureSetID\\\": \\\"69f5a1f5-b26c-5ef6-42ca-08ec6fc8ba64\\\"\\n  }\\n ]\\n}\":\"\"}}"


var jo = JObject.Parse(jsonString); =>

{
  "{\n \"UnitOfMeasureRelatedUnitDataInbound\": ": {
    "\n  {\n   \"Name\": \"test\",\n   \"Active\": true,\n   \"UnitOfMeasureTypeID\": \"dd89f0a0-59c3-49a1-a2ae-7e763da32065\",\n   \"BaseUnitID\": \"4c835ebb-60f2-435f-a5f4-8dc311fbbca0\",\n   \"BaseUnitName\": null,\n   \"BaseUnitAbbreviation\": null,\n   \"RelatedUnitDisplayOrder\": 1,\n   \"RelatedUnitName\": \"Inch\",\n   \"RelatedUnitAbbreviation\": \"in\",\n   \"RelatedUnitConversionRatio\": 25.4,\n   \"UnitOfMeasureSetID\": \"69f5a1f5-b26c-5ef6-42ca-08ec6fc8ba64\"\n  },\n  {\n   \"Name\": \"test\",\n   \"Active\": true,\n   \"UnitOfMeasureTypeID\": \"dd89f0a0-59c3-49a1-a2ae-7e763da32065\",\n   \"BaseUnitID\": \"4c835ebb-60f2-435f-a5f4-8dc311fbbca0\",\n   \"BaseUnitName\": null,\n   \"BaseUnitAbbreviation\": null,\n   \"RelatedUnitDisplayOrder\": 2,\n   \"RelatedUnitName\": \"Foot\",\n   \"RelatedUnitAbbreviation\": \"ft\",\n   \"RelatedUnitConversionRatio\": 304.8,\n   \"UnitOfMeasureSetID\": \"69f5a1f5-b26c-5ef6-42ca-08ec6fc8ba64\"\n  },\n  {\n   \"Name\": \"test\",\n   \"Active\": true,\n   \"UnitOfMeasureTypeID\": \"dd89f0a0-59c3-49a1-a2ae-7e763da32065\",\n   \"BaseUnitID\": \"4c835ebb-60f2-435f-a5f4-8dc311fbbca0\",\n   \"BaseUnitName\": null,\n   \"BaseUnitAbbreviation\": null,\n   \"RelatedUnitDisplayOrder\": 3,\n   \"RelatedUnitName\": \"Yard\",\n   \"RelatedUnitAbbreviation\": \"yd\",\n   \"RelatedUnitConversionRatio\": 914.4,\n   \"UnitOfMeasureSetID\": \"69f5a1f5-b26c-5ef6-42ca-08ec6fc8ba64\"\n  },\n  {\n   \"Name\": \"test\",\n   \"Active\": true,\n   \"UnitOfMeasureTypeID\": \"dd89f0a0-59c3-49a1-a2ae-7e763da32065\",\n   \"BaseUnitID\": \"4c835ebb-60f2-435f-a5f4-8dc311fbbca0\",\n   \"BaseUnitName\": null,\n   \"BaseUnitAbbreviation\": null,\n   \"RelatedUnitDisplayOrder\": 4,\n   \"RelatedUnitName\": \"Centimeter\",\n   \"RelatedUnitAbbreviation\": \"cm\",\n   \"RelatedUnitConversionRatio\": 10,\n   \"UnitOfMeasureSetID\": \"69f5a1f5-b26c-5ef6-42ca-08ec6fc8ba64\"\n  },\n  {\n   \"Name\": \"test\",\n   \"Active\": true,\n   \"UnitOfMeasureTypeID\": \"dd89f0a0-59c3-49a1-a2ae-7e763da32065\",\n   \"BaseUnitID\": \"4c835ebb-60f2-435f-a5f4-8dc311fbbca0\",\n   \"BaseUnitName\": null,\n   \"BaseUnitAbbreviation\": null,\n   \"RelatedUnitDisplayOrder\": 5,\n   \"RelatedUnitName\": \"Decimeter\",\n   \"RelatedUnitAbbreviation\": \"dm\",\n   \"RelatedUnitConversionRatio\": 100,\n   \"UnitOfMeasureSetID\": \"69f5a1f5-b26c-5ef6-42ca-08ec6fc8ba64\"\n  },\n  {\n   \"Name\": \"test\",\n   \"Active\": true,\n   \"UnitOfMeasureTypeID\": \"dd89f0a0-59c3-49a1-a2ae-7e763da32065\",\n   \"BaseUnitID\": \"4c835ebb-60f2-435f-a5f4-8dc311fbbca0\",\n   \"BaseUnitName\": null,\n   \"BaseUnitAbbreviation\": null,\n   \"RelatedUnitDisplayOrder\": 6,\n   \"RelatedUnitName\": \"Meter\",\n   \"RelatedUnitAbbreviation\": \"m\",\n   \"RelatedUnitConversionRatio\": 1000,\n   \"UnitOfMeasureSetID\": \"69f5a1f5-b26c-5ef6-42ca-08ec6fc8ba64\"\n  }\n ]\n}": ""
  }
}


var _UnitOfMeasureRelatedUnitData = jo["UnitOfMeasureRelatedUnitDataInbound"]; => null(假定为6个元素)

List<UnitsOfMeasure.UnitOfMeasureRelatedUnitDataInbound> _UnitOfMeasureRelatedUnitDataList = JsonConvert.DeserializeObject<List<UnitsOfMeasure.UnitOfMeasureRelatedUnitDataInbound>>(_UnitOfMeasureRelatedUnitData.ToString());(导致以下错误)=>


  Admin.Web.API.dll中发生类型'System.NullReferenceException'的异常,但未在用户代码中处理。附加信息:对象引用未设置为对象的实例。


完整的代码:



var _UnitOfMeasureRelatedUnitData = [];
var rows = $('#jqxUOMRelatedUnitsDropdownGrid').jqxGrid('getrows');
var RecordCount = 0;

if (rows.length !== 1 && rows[0]["UOMRelatedUnit_Name"] !== "") {
  for (var i = 0; i < rows.length; i++) {
    var row = rows[i];
    var _row = {};
    if (row.UOMRelatedUnit_AddItem) {
      RecordCount += 1;
      _row["Name"] = $("#txtUnitOfMeasureSetName").val();
      _row["Active"] = T;
      _row["UnitOfMeasureTypeID"] = $("input[type='radio'][id='rblUnitOfMeasureType']:checked").val();
      _row["BaseUnitID"] = $("input[type='radio'][id='rblUnitOfMeasureBaseUnit']:checked").val();
      _row["BaseUnitName"] = null;
      _row["BaseUnitAbbreviation"] = null;
      _row["RelatedUnitDisplayOrder"] = RecordCount;
      _row["RelatedUnitName"] = row.UOMRelatedUnit_Name;
      _row["RelatedUnitAbbreviation"] = row.UOMRelatedUnit_Abbreviation;
      _row["RelatedUnitConversionRatio"] = row.UOMRelatedUnit_ConversionOfBaseUnits;
      _row["UnitOfMeasureSetID"] = UnitOfMeasureSetID;
      _UnitOfMeasureRelatedUnitData[i] = _row;
    }
  }

  var jsonData = JSON.stringify({
    UnitOfMeasureRelatedUnitDataInbound: _UnitOfMeasureRelatedUnitData
  }, null, ' ');

  callService("POST", g_WebServiceUnitOfMeasureRelatedUnitsCreateURL, jsonData, 'json', function(jsonResult) {});
}





WEB.API代码:

[Route("api/admin/UnitsOfMeasure/UnitOfMeasureRelatedUnitsCreate")]
[HttpPost]
[ResponseType(typeof(UnitsOfMeasure.UnitsOfMeasureDataWithMessage))]
public IHttpActionResult UnitOfMeasureRelatedUnitsCreate([FromBody] JObject UnitOfMeasureRelatedUnitDataInbound)
{
    var jsonString = JsonConvert.SerializeObject(UnitOfMeasureRelatedUnitDataInbound);
    var jo = JObject.Parse(jsonString);
    var _UnitOfMeasureRelatedUnitData = jo["UnitOfMeasureRelatedUnitDataInbound"];
    List<UnitsOfMeasure.UnitOfMeasureRelatedUnitDataInbound> _UnitOfMeasureRelatedUnitDataList = JsonConvert.DeserializeObject<List<UnitsOfMeasure.UnitOfMeasureRelatedUnitDataInbound>>(_UnitOfMeasureRelatedUnitData.ToString());

    UnitsOfMeasure _UnitsOfMeasure = new UnitsOfMeasure();
    UnitsOfMeasure.UnitsOfMeasureDataWithMessage _UnitsOfMeasureDataWithMessage = _UnitsOfMeasure.UnitOfMeasureRelatedUnits_Create(_UnitOfMeasureRelatedUnitDataList);

    return Ok(_UnitsOfMeasureDataWithMessage);
}


资料类别:

public class UnitOfMeasureRelatedUnitDataInbound
{
    public string Name { get; set; }
    public Nullable<bool> Active { get; set; }
    public Nullable<Guid> UnitOfMeasureTypeID { get; set; }
    public Nullable<Guid> BaseUnitID { get; set; }
    public string BaseUnitName { get; set; }
    public string BaseUnitAbbreviation { get; set; }
    public Nullable<int> RelatedUnitDisplayOrder { get; set; }
    public string RelatedUnitName { get; set; }
    public string RelatedUnitAbbreviation { get; set; }
    public Nullable<double> RelatedUnitConversionRatio { get; set; }
    public Nullable<Guid> UnitOfMeasureSetID { get; set; }
}


和DAL:

public UnitsOfMeasureDataWithMessage UnitOfMeasureRelatedUnits_Create(List<UnitOfMeasureRelatedUnitDataInbound> _UnitOfMeasureRelatedUnitDataInbound)
{
    UnitsOfMeasureDataWithMessage _oUnitsOfMeasureDataWithMessage = new UnitsOfMeasureDataWithMessage();
    return _oUnitsOfMeasureDataWithMessage;
}


我的问题是:


为什么每次迭代都更改JSON字符串?
有没有一种方法可以简单地以编程方式使入站JSON字符串通用,以使此功能正常工作?
如果有办法,那是什么?有没有可以帮助您的例子(我找不到)?

最佳答案

找到了解决方法。显然,我试图通过使问题变得过于复杂来努力实现这一目标。

http://kwilson.me.uk/blog/post-an-array-of-objects-to-webapi-using-jquery/

09-25 21:48