如何创建一个JavaScript数组

如何创建一个JavaScript数组

我对此并不陌生,所以请多多包涵。

请指导我如何创建一个javascript数组,这反过来又给了我这样的json。

{
"MotorInsurance": [{
    "Service": "Compare",
    "Data": [{
        "Apikey": "1234",
        "Process": "Compare",
        "TransactionId": "32",
        "Type": "New",
        "Channel": "1"
    }],
    "Vehicle": [{
        "VehicleCode": "456",
        "RTOCode": "AP12",
        "RegistrationYear": "2016"
    }],
    "User":[{
         "IPAddress": "66",
         "DateTime": "12-06-2016"
    }]
}]

}


我已经试过了:

var formData = {};
formData['MotorInsurance'] = {};
formData['MotorInsurance']['Service'] = "Compare";
formData['MotorInsurance']['Data'] = {};
formData['MotorInsurance']['Data']['Apikey'] = '1234';
formData['MotorInsurance']['Data']['Process'] = 'Compare';
formData['MotorInsurance']['Data']['TransactionId'] = '32';
formData['MotorInsurance']['Data']['Type'] = 'New';
formData['MotorInsurance']['Data']['Channel'] = '1';
formData['MotorInsurance']['Vehicle'] = {};
formData['MotorInsurance']['Vehicle']['VehicleCode'] = '';
formData['MotorInsurance']['Vehicle']['RTOCode'] = '';
formData['MotorInsurance']['Vehicle']['RegistrationYear'] = '';
formData['MotorInsurance']['User'] = {};
formData['MotorInsurance']['User']['IPAddress'] = '66.12.5.4';
formData['MotorInsurance']['User']['DateTime'] = '12-06-2016';


请指导我。谢谢

最佳答案

var formData = {};
formData['MotorInsurance'] = [{}];
formData['MotorInsurance'][0]['Service'] = "Compare";
formData['MotorInsurance'][0]['Data'] = [{}];
formData['MotorInsurance'][0]['Data'][0]['Apikey'] = '1234';
formData['MotorInsurance'][0]['Data'][0]['Process'] = 'Compare';
formData['MotorInsurance'][0]['Data'][0]['TransactionId'] = '32';
formData['MotorInsurance'][0]['Data'][0]['Type'] = 'New';
formData['MotorInsurance'][0]['Data'][0]['Channel'] = '1';
formData['MotorInsurance'][0]['Vehicle'] = [{}];
formData['MotorInsurance'][0]['Vehicle'][0]['VehicleCode'] = '';
formData['MotorInsurance'][0]['Vehicle'][0]['RTOCode'] = '';
formData['MotorInsurance'][0]['Vehicle'][0]['RegistrationYear'] = '';
formData['MotorInsurance'][0]['User'] = [{}];
formData['MotorInsurance'][0]['User'][0]['IPAddress'] = '66.12.5.4';
formData['MotorInsurance'][0]['User'][0]['DateTime'] = '12-06-2016';
document.write('<pre>' + JSON.stringify(formData, 0, 4) + '</pre>');

09-19 03:17