Convert an array of objects to JSON

3

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

    
asked by Veleras 09.05.2018 в 17:35
source

1 answer

1

The detail is that in the variable vjson2 you are storing the result of the function each and you only need the arrangement of objects that are modified inside it.

The result you get from each is a JQuery object, so you notice the keys and additional properties.

Once you called the eval function on the value of the hidden field, it is no longer necessary to perform another transformation because the result is already a javascript object, in your particular case, an array of objects.

Therefore, this code:

'$'(jQuery.parseJSON(JSON.stringify(vjson)))

It can be replaced by the following:

$(vjson)

The resulting code would look like this:

function ModificarDato(jsonelemento, jsonvalor)
{
    var vjson = eval($("[ id$='H_Ins").val());
    $(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(vjson);
   $("[ id$=H_Ins ]").val(jsonvalorfinal);
}

Note that the variable vjson2 is no longer used because it is not necessary.

Here the fiddle to check: link

    
answered by 09.05.2018 / 18:37
source