问题
I'm receiving JSON from a 3rd party and need to parse quantity conditionally... Depending on the type of usage of the line item, "ReplacementCount" or "ServiceCount" will need to become "quantity", and delete or ignore the other. THere's no case where both 'lineitemfieldname' will be > 0, it will always be one or the other.
I'm making it to my first if statement, but never into the second... I am pretty certain that I'm handling the if statement for the JSON object/value incorrectly, but am not sure how to resolve.
Here's my sample JSON:
{"recordtype":"salesorder","item":
[{"InventoryManagementKey":"20001","InvoiceDay":"9/10/2015","ReplacementAmount":0.0000,"ReplacementCount":500,"ServiceAmount":0.0000,"ServiceCount":0}]}
Here's the fragment of server-side script handling the JSON:
for(var lineitemfieldname in lineitemobject)
{
var lineitemfieldvalue = lineitemobject[lineitemfieldname];
if(lineitemfieldname == 'ServiceCount' && lineitemfieldvalue != 0)
{
if(lineitemfieldname == 'InventoryManagementKey')
{
lineitemfieldname = 'item';
}
if(lineitemfieldname == 'ServiceCount')
{
lineitemfieldname = 'quantity';
}
delete 'ServiceAmount';
delete 'ReplacementAmount';
delete 'ReplacementCount';
delete 'invoiceDay';
record.setCurrentLineItemValue('item',lineitemfieldname,lineitemfieldvalue);
}
}
回答1:
This will update your json based on "ReplacementAmount"
var lineitemobject = JSON.parse('{"recordtype":"salesorder","item":[{"InventoryManagementKey":"20001","InvoiceDay":"9/10/2015","ReplacementAmount":0.0000,"ReplacementCount":500,"ServiceAmount":0.0000,"ServiceCount":0}]}');
if(lineitemobject.item[0]['ReplacementAmount'] > 0) {
lineitemobject.item[0]['quantity'] = lineitemobject.item[0]['ReplacementAmount'];
delete lineitemobject.item[0]['ReplacementAmount'];
} else {
lineitemobject.item[0]['quantity'] = lineitemobject.item[0]['ServiceCount'];
delete lineitemobject.item[0]['ServiceCount'];
}
Here only lineitemobject JavaScript object value changes, not the JSON string.
回答2:
I was able to resolve this thanks to rajuGT's guidance. For anyone who stumbles upon this, here's a summary of my resolution:
var currentValue = 0; //Global
for (var itemobject in value){
record.selectNewLineItem('item');
var lineitemobject = value[itemobject];
for(var lineitemfieldname in lineitemobject){
var lineitemfieldvalue = lineitemobject[lineitemfieldname];
if(lineitemfieldname == 'ReplacementCount'){
if(lineitemfieldvalue != 0){
lineitemfieldname = 'quantity';
currentValue = lineitemfieldvalue;
}
delete 'ServiceCount';
}
else if (lineitemfieldname == 'ServiceCount'){
if(lineitemfieldvalue != 0){
lineitemfieldname = 'quantity';
currentValue = lineitemfieldvalue;
}
delete 'ReplacementCount';
}
if(lineitemfieldname == 'InventoryManagementKey'){
lineitemfieldname = 'item';
}
record.setCurrentLineItemValue('item',lineitemfieldname,lineitemfieldvalue);
record.setCurrentLineItemValue('item','price','9')
}
record.commitLineItem('item');
}
回答3:
Validating the JSON Schema Draft-07, JSON now supports the if...then...else
keywords for conditional data representation.
{
"type": "integer",
"minimum": 1,
"maximum": 1000,
"if": { "minimum": 100 },
"then": { "multipleOf": 100 },
"else": {
"if": { "minimum": 10 },
"then": { "multipleOf": 10 }
}
}
来源:https://stackoverflow.com/questions/32637851/json-if-statement