0

I am creating some JSON data to be validated through an ajax call.

Here's my constructed JSON :

{"telephone1":"66",
 "telephone2":"66",
 "fax":"66",
 "mobilePhone":"66",
 "dateEffectiveChangementAdresseOuTel":"66",
 "adresse1IdentiqueAdresse2":true}

Here's the one obtained :

{ "adresse1IdentiqueAdresse2" : true,
  "dateEffectiveChangementAdresseOuTel" : "66",
  "fax" : "66",
  "mobilePhone" : "66",
  "telephone1" : "66",
  "telephone2" : "66"
}

As you can notice, my key are reordered in alphabetical order, which I do not want.

This causes errors to be returned to the page in the 2nd order, but I need them in the 1st order. I want my error summary (Html.ValidationSummary) to follow the errors on the page (1st error = 1st field in error).

Is there any way to preserve my original order?
Or someway to bypass this?

edit

        var coord = {
            telephone1: $("#Telephone1").val(),
            telephone2: $("#Telephone2").val(),
            fax: $("#Fax").val(),
            mobilePhone: $("#MobilePhone").val(),
            dateEffectiveChangementAdresseOuTel: $("#DateEffectiveChangementAdresseOuTel").val(),
            adresse1IdentiqueAdresse2: $("#Adresse1IdentiqueAdresse2").is(":checked")
        };

        $.ajax({
            type: 'POST',
            url: urlControleur + '_ActionTransmettre',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            data: JSON.stringify(coord),
            success: function (data, textStatus, jqXHR) {
                if (typeof (data) == "string") {
                    window.location = data
                    MsgErreur("");
                }
                else {
                    ListeMsgErreur(data);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                handleAjaxError(XMLHttpRequest, "M000017");
            }
        });

ajax call return (in error)

["The value {0} is not valid for Effective.",
"Le numéro saisi doit respecter le format 999 999-9999",
"Le numéro saisi doit respecter le format 999 999-9999",
"Le numéro saisi doit respecter le format 999 999-9999 ou, si vous devez saisir un numéro de poste, le format est 999 999-9999 x 9999999.",
"Le numéro saisi doit respecter le format 999 999-9999"]

It's impossible to reorder the return as is.

Kraz
  • 6,910
  • 6
  • 42
  • 70

1 Answers1

0

I would review the code that returns the json. this is where it's happening. if you cannot change the code which renders the JSON then remodel the data before passing it along

return {
   telephone1: theJson.telephone1,
   telephone2: theJson.telephone2,
   fax: theJson.fax,
   mobilePhone: theJson.mobilePhone,
   dateEffectiveChangementAdresseOuTel: theJson.dateEffectiveChangementAdresseOuTel,
   adresse1IdentiqueAdresse2: thsJson.adresse1IdentiqueAdresse2
};
Jason Meckley
  • 7,589
  • 1
  • 24
  • 45
  • Thanks for your suggestion, I added information to my question, as you can notice, unless I change the server-side actions, which is not wanted, it wouldn't work. – Kraz Mar 01 '12 at 17:09
  • I'll accept this as the answer as it is one possible answer. The other would have been to do the same logic server-side, upon reception of the json data. That or other overly complicated solutions. No easy way out it seems! – Kraz Mar 01 '12 at 21:23
  • i would question why the properties must be in a certain order, and if so go to the source and order them there. this way you don't need to re-map values. – Jason Meckley Mar 01 '12 at 22:29
  • They need to be ordered for display purpose only (when displaying the validation summary). – Kraz Mar 02 '12 at 17:43