Javascript 在MVC控制器中解析JSON时JSON原语无效
我正在JavaScript中创建一个JSON字符串,并将其发送到mvc应用程序中的控制器。创建JSON字符串的代码:Javascript 在MVC控制器中解析JSON时JSON原语无效,javascript,asp.net-mvc,json,asp.net-mvc-3,asp.net-mvc-4,Javascript,Asp.net Mvc,Json,Asp.net Mvc 3,Asp.net Mvc 4,我正在JavaScript中创建一个JSON字符串,并将其发送到mvc应用程序中的控制器。创建JSON字符串的代码: $('#btnassign').click(function () { var arrPrice = ""; var arrMarkUP = ""; $("table tr").each(function () { if ($(this).find('input
$('#btnassign').click(function () {
var arrPrice = "";
var arrMarkUP = "";
$("table tr").each(function () {
if ($(this).find('input:checkbox:first').is(':checked')) {
if ($(this).find('input.inputprice').val() != "") {
arrPrice += "{";
var price = $(this).find('input.inputprice').val();
var id = $(this).find('input[type=hidden]').val();
arrPrice += '"Id":"' + id + '","Price":"' + price + '"';
arrPrice += "},";
}
if ($(this).find('input.inputmarkup').val() != "") {
arrMarkUP += "{";
var price = $(this).find('input.inputmarkup').val();
var id = $(this).find('input[type=hidden]').val();
arrMarkUP += "Id:" + id + ",Price:" + price;
arrMarkUP += "},";
}
}
});
var lastindexp = arrPrice.lastIndexOf(",");
arrPrice = arrPrice.substring(0, lastindexp) + "|";
var lastindexm = arrMarkUP.lastIndexOf(",");
arrMarkUP = arrMarkUP.substring(0, lastindexm) + "|";
alert(arrPrice);
alert(arrMarkUP);
$("#hdPrice").val(arrPrice);
$("#hdMarkUP").val(arrMarkUP);
$("#AssignProductForm").submit();
});
});
我的JSON字符串由上述代码生成:
{"Id":"1","Price":"4"},{"Id":"2","Price":"6"}
我的控制器解析代码:
[HttpPost]
public ActionResult AddProducts(FormCollection collection, string txtsearch)
{
var ManualPricing = collection["hdPrice"].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
JavaScriptSerializer ser = new JavaScriptSerializer();
var Manual = ser.Deserialize<PriceMargin>(ManualPricing[0]);
}
行var Manual=ser.反序列化(ManualPricing[0])
给出了一个错误:
无效的JSON原语:{“Id”:“2”,“Price”:“9”}
解决方案:
var list = new JavaScriptSerializer().Deserialize<List<KeyValue>>(json);
public class KeyValue
{
public string key;
public string value;
}
var list=new JavaScriptSerializer()。反序列化(json);
公共类键值
{
公共字符串密钥;
公共字符串值;
}
您的JSON无效
JSON只能有一个根对象
如果要有多个对象,请序列化一个数组。要跟进注释,如果要创建一个对象数组,可以执行以下操作:
pricesArray = [];
$("table tr").each(function () {
if ($(this).find('input:checkbox:first').is(':checked')) {
var price = $(this).find('input.inputprice').val();
var id = $(this).find('input[type=hidden]').val();
pricesArray.push({Id : id, Price: price});
}
});
// pricesArray [{"Id":"1","Price":"4"},{"Id":"2","Price":"6"}]
pricesArray
现在应该包含您想要的数据,序列化和取消序列化数据并发送数据应该会容易得多上次我遇到这个错误时,我试图将对象值直接传递给json数据。。。i、 e
data = { datavalue: @Object.valueField};
Json格式
$.ajax({
type: "POST",
url: "/Controller/FunctionToPostTo",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (response) {
alert('Success');
},
failure: function (response) {
alert('Failure');
alert(response.responseText);
},
error: function (response) {
alert('Error');
alert(response.responseText);
}
});
$.ajax({
type: "POST",
url: "/Controller/FunctionToPostTo",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (response) {
alert('Success');
},
failure: function (response) {
alert('Failure');
alert(response.responseText);
},
error: function (response) {
alert('Error');
alert(response.responseText);
}
});
为了解决这个问题,我创建了一个单独的变量并给它赋值,
在Ajax中传递数据值时,我使用了JSON.stringify函数
var variableHolder = @Object.valueField;
data = { datavalue: variableHolder };
Json格式
$.ajax({
type: "POST",
url: "/Controller/FunctionToPostTo",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (response) {
alert('Success');
},
failure: function (response) {
alert('Failure');
alert(response.responseText);
},
error: function (response) {
alert('Error');
alert(response.responseText);
}
});
$.ajax({
type: "POST",
url: "/Controller/FunctionToPostTo",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (response) {
alert('Success');
},
failure: function (response) {
alert('Failure');
alert(response.responseText);
},
error: function (response) {
alert('Error');
alert(response.responseText);
}
});
然后问题消失了为什么要手动构建字符串?您可以使用本机数据类型,然后将这些数据类型编码为JSON strong,并将JSON字符串解码为本机数据类型。我不知道怎么做,我不熟悉javascript,jquery!!任何示例?添加了使用javascript对象的示例