JSON If陈述式 [英] JSON If Statement

查看:141
本文介绍了JSON If陈述式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从第三方接收JSON,需要有条件地解析数量...根据订单项的使用类型,"ReplacementCount"或"ServiceCount"将需要变为"quantity",然后删除或忽略对方.在任何情况下,两个"lineitemfieldname"都不会大于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 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.

我正在第一个if语句中使用它,但是从不进入第二个if语句...我可以肯定我对JSON对象/值的if语句处理不正确,但是不确定如何解决.

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.

这是我的示例JSON:

Here's my sample JSON:

{"recordtype":"salesorder","item":
 [{"InventoryManagementKey":"20001","InvoiceDay":"9/10/2015","ReplacementAmount":0.0000,"ReplacementCount":500,"ServiceAmount":0.0000,"ServiceCount":0}]}

这是处理JSON的服务器端脚本的片段:

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

推荐答案

这将基于"ReplacementAmount"更新您的json

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'];
}

这里仅更改lineitemobject JavaScript对象的值,而不更改JSON字符串.

Here only lineitemobject JavaScript object value changes, not the JSON string.

这篇关于JSON If陈述式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆