#javascript #jquery #html #json
#javascript #jquery #HTML #json
Вопрос:
как удалить эти инвертированные «», которые создаются автоматически, когда я строю объект Json.?
{"entityList":["{"Scenario No":"SC1","Accounting Mon Extracted":"null","Acronym":"null","Address1":"542","Address2":"null","AddressType
и затем, когда запускается следующий сценарий, создается еще один » «… я хочу, чтобы он был такого типа, чтобы я мог отправить его на сервер…
{
"entityList": [
{
"scenarioNo": "SC1",
"accountingMonExtracted": null,
"acronym": null,
"address1": "542 W 27th Street 4th Floor",
"address2": null,
"addressType": "MAILING",
"alternateEmailId": null,
"alternateFaxNo": null,
"alternatePhoneNo": null,
"city": "Bethlehem",
"country": "US",
"county": null,
"firstName": "Thomas",
"fullName": "Peter Jen",
"lastName": "Routher",
"middleName": null,
"namePrefix": null,
"nameSuffix": null,
"primaryAddressYN": "Y",
"primaryEmailId": "johnmann@yahoo.com",
"primaryFaxNo": "(406)485-2983",
"primaryPhoneNo": "(405)452-3645",
"state": "AB",
"zipCode": "40007.0",
"agentType": null,
"autoCommClosingDays": null,
"autoCommExtractionYN": null,
"billingContactName": "Peter Jen",
"commissionCurrency": null,
"commissionEFTTokenId": null,
"commissionEntityYN": null,
"commissionPayeeEntityCode": null,
"commissionPayeeEntityType": null,
"commissionPaymentMethod": null,
"commissionStatementEntityCode": null,
"commissionStatementEntityType": null,
"creditTermDays": null,
"creditTermMonths": null,
"customerNo": null,
"doingBusinessAsName": null,
"eftDraftOption": null,
"entityType": "INSURED",
"frequencyDayMonth": null,
"frequencyDayOfWeek": null,
"frequencyType": null,
"invoiceCurrency": "USD",
"ncanGraceDays": null,
"outputDeliveryMethod": "M",
"parentEntityCode": null,
"paymentMethod": "EFT",
"process1099Indicator": "N",
"recurringEFTTokend": null,
"requestedDueDay": null,
"sourceSystem": "DPAS",
"sourceSystemEntityCode": "INS000020",
"statementFormat": null,
"statementIndicatorYN": "N",
"statementMailingDay": null,
"userRemarks": null,
"vendorCode": null,
"ssn": null,
"taxid": null
},
{
"scenarioNo": "SC2",
"accountingMonExtracted": null,
"acronym": null,
"address1": "542 W 27th Street 4th Floor",
"address2": null,
"addressType": "MAILING",
"alternateEmailId": null,
"alternateFaxNo": null,
"alternatePhoneNo": null,
"city": "Bethlehem",
"country": "US",
Мой код на javascript выглядит следующим образом…
var row = {};
var insured=[];
var a;
var row1 = {};
var insured1=[];
var a1;
function creatJsonobj(){
var cols = $("#policytable thead tr th").map(function(){
return $(this).text()
})
var tableObject = $("#policytable tbody tr").map(function(i) {
$(this).find("td").each(function(i) {
var rowName = cols[i];
// console.log(rowName);
row[rowName] = $(this).find('input').val();
a= JSON.stringify(row);
})
// console.log("entity table : " a);
insured.push(a);
// console.log(insured);
return row;
});
for(var i in insured){
var cli = JSON.parse(insured[i]);
// console.log(cli);
}
var entityObj = {entityList: insured} ;
console.log(JSON.stringify(entityObj).replace(/\/g, ''));
$("#entityTable").empty();
var cols1 = $("#paymenttable thead tr th").map(function(){
return $(this).text()
})
var tableObject = $("#paymenttable tbody tr").map(function(i) {
$(this).find("td").each(function(i) {
var rowName = cols1[i];
// console.log(rowName);
row1[rowName] = $(this).find('input').val();
a1= JSON.stringify(row1);
})
// console.log("payment table : " a1);
insured1.push(a1);
return row1;
});
for(var i in insured1){
var cli = JSON.parse(insured1[i]);
}
var paymentObj = {paymentList:insured1} ;
/* var C=JSON.stringify(sendJson1);
var finalData = C.replace(/\/g, '');
alert(finalData);
console.log(finalData);*/
console.log(paymentObj);
var billing = {entityObj,paymentObj};
console.log(billing);
Как это сделать ….??
Комментарии:
1. Вы можете использовать
""
вместоnull
и проблема решена2.
a= JSON.stringify(row);
должно произойти вне вашего.each()
цикла.
Ответ №1:
Когда вы stringifyied row
, теперь это a string
, а не a object
.
Ваш способ:
var row = JSON.stringify({key:value}); // which return a string
var object = {entityList: [row]};
console.log(JSON.stringify(object)); // which inputs "{entityList: ["{"key":"value"}"]}"
Правильный путь:
var object = {entityList: [row]};
console.log(JSON.stringify(object));