为什么我的JSON数据在传递给WEB.API Ajax调用时会被更改?

时间:2015-06-23 09:12:05

标签: javascript c# jquery ajax json

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());(导致以下错误的结果)=&gt;

  

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

我的问题是:

  1. 为什么每次迭代都会更改JSON字符串?
  2. 是否可以通过编程方式简单地将入站JSON字符串设置为通用字符串以使其正常工作?
  3. 如果有办法,那里有什么或有什么例子(我无法找到)可以提供帮助?

1 个答案:

答案 0 :(得分:0)

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

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