JSON If Statement

2019-08-31 03:05发布

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);
                                }
}

3条回答
疯言疯语
2楼-- · 2019-08-31 03:43

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楼-- · 2019-08-31 03:52

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.

查看更多
贼婆χ
4楼-- · 2019-08-31 03:59

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 }
    }
}
查看更多
登录 后发表回答