You see, I have the following problem,
I have a field of type hidden
on a page asp.net
that has as value a list of objects serialized with JSON
from C#
. The value of hidden
is the following text:
[{ "Element":"E001", "City":"Madrid", "Country":"Spain"},
{ "Element":"E003", "City":"Paris", "Country":"Italy"},
{ "Element":"A001", "City":"Pekin", "Country":"China"}]
This list is variable, sometimes there will be 3 elements other 100, other 46, we will change.
I have defined the following function in javascript
that is called when the user clicks on a certain button or icon, for example, in this case I would press the country flag francia
and call ModificarDato("E003", "Francia");
function ModificarDato(jsonelemento, jsonvalor)
{
var vjson = eval($("[ id$='H_Ins").val());
var vjson2 = '$'(jQuery.parseJSON(JSON.stringify(vjson))).each(function ()
{
var vElemento = this.Element;
var vCity = this.City;
var vValor = this.Country;
if (vElemento == jsonelemento)
{
this.Country = jsonvalor;
}
});
var jsonvalorfinal = JSON.stringify(vjson2);
$("[ id$=H_Ins ]").val(jsonvalorfinal);
}
The fact is that when I try to write the last 2 lines the text that is put to the field is not the same but has introduced more keys and parameters that should not be, the result is:
{"0":{"Element":"E001", "City":"Madrid", "Country":"Spain"},
"1":{"Element":"E003", "City":"Paris", "Country":"France"},
"2":{"Element":"A003", "City":"Pekin", "Country":"China"},
"length":3}
when the result should be this other
[{ "Element":"E001", "City":"Madrid", "Country":"Spain"},
{ "Element":"E003", "City":"Paris", "Country":"France"},
{ "Element":"A001", "City":"Pekin", "Country":"China"}]
The modification is done correctly, that is, it finds the element and modifies the value, but when I want to convert it to plain text to put it in the hidden
and I can press another button if you want, the text to which I have put it has changed with a position identifier and a length.
Any idea why you are doing this to me? Is it the same thing to do, but when you pick it up from code behind it will not correspond to the% co_of% of objects that I expect.
Thank you very much