2
Good evening! I have the following script in my View:
$('form').submit(function(e){
e.preventDefault();
var resposta = new Array();
var x = 0;
var y = 0;
$(".resposta").each(function () {
var dados = new Array();
dados[x] = $(this).children("input[name^='idQuestao']").serialize();
dados[++x] = $(this).children("input[name^='idQuestionario']").serialize();
dados[++x] = $(this).children("input[name^='idCampanha']").serialize();
dados[++x] = $(this).children("input[name^='idFuncionario']").serialize();
dados[++x] = $(this).find("input[name^='idOpcao']").serialize();
if(dados[x] === "")
{
dados[x] = $(this).find("select[name^='idOpcao']").serialize();
}
if(dados[x] === "")
{
dados[x] = $(this).find("textarea[name^='descricao']").serialize();
}
resposta[y] = dados;
x = 0;
y++;
});
$.ajax({
type: "POST",
url: '/Questionario/Create/?questionario=' + JSON.stringify(resposta),
success: function(result) {
},
error: function(result) {
}
});
});
In my Controller I am trying to receive as follows:
public ActionResult Create(string questionario)
{
try
{
var intermediateJson = JsonConvert.DeserializeObject<string>(questionario);
var oPerson = JsonConvert.DeserializeObject<List<QuestionarioViewModel>>(intermediateJson);
return View();
}
catch(Exception ex)
{
return View();
}
}
and I’m getting the following error: "Unexpected character encountered while parsing value: [. Path '', line 1, position 1."
This is the string the controller is receiving:
"[[\"idQuestao=1\",\"idQuestionario=1\",\"idCampanha=2\",\"idFuncionario=1\",\"descricao=teste\"],[\"idQuestao=2\",\"idQuestionario=1\",\"idCampanha=2\",\"idFuncionario=1\",\"descricao=teste\"],[\"idQuestao=3\",\"idQuestionario=1\",\"idCampanha=2\",\"idFuncionario=1\",\"idOpcao=2\"],[\"idQuestao=4\",\"idQuestionario=1\",\"idCampanha=2\",\"idFuncionario=1\",\"idOpcao=6\"],[\"idQuestao=5\",\"idQuestionario=1\",\"idCampanha=2\",\"idFuncionario=1\",\"idOpcao=11\"],[\"idQuestao=8\",\"idQuestionario=1\",\"idCampanha=2\",\"idFuncionario=1\",\"descricao=teste\"]]"
I’ve tried several ways, what I’m doing wrong?
Try to change the
[ & ]
for{ & }
, because in JSON, [ and ] are not valid key characters. You can useX = X.Replace("[", "{").Replace("]", "}")
in this case.– CypherPotato
@T. Borges If any answer has been useful to you, please mark it as accepted
√
, so when other users view your question they will see that you already have a correct answer and accept it for you.– Alisson Marqui