最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSON If Statement - Stack Overflow

programmeradmin2浏览0评论

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

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 bee "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);
                                }
}
Share edited Sep 17, 2015 at 21:38 rajuGT 6,4242 gold badges28 silver badges44 bronze badges asked Sep 17, 2015 at 18:47 MBrewerMBrewer 811 gold badge2 silver badges10 bronze badges 4
  • finally, which fields you want to access? and what is the expected output? – rajuGT Commented Sep 17, 2015 at 18:59
  • If the value present for 'ReplacementCount' > 0, the label 'ReplacementCount' should be changed to 'quantity' This can move to either an if/else, or keep the second if... but if 'ReplacementCount' = 0, 'ServiceCount' should be changed to 'quantity' – MBrewer Commented Sep 17, 2015 at 19:03
  • check the updated one now – rajuGT Commented Sep 17, 2015 at 19:14
  • SyntaxError: Unexpected token: o (Create Sales Order Restlet.js$1524488#48) – MBrewer Commented Sep 17, 2015 at 19:50
Add a ment  | 

3 Answers 3

Reset to default 2

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

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.

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.mitLineItem('item');
}
发布评论

评论列表(0)

  1. 暂无评论