T100——FGL与JSON之间的转换

FGL ==========>>>>>> JSON ===========>>> String:

IMPORT util
MAIN
    DEFINE cust_rec RECORD
               cust_num INTEGER,
               cust_name VARCHAR(30),
               order_ids DYNAMIC ARRAY OF INTEGER
           END RECORD
    DEFINE obj util.JSONObject
    LET cust_rec.cust_num = 345
    LET cust_rec.cust_name = "McMaclum"
    LET cust_rec.order_ids[1] = 4732
    LET cust_rec.order_ids[2] = 9834
    LET cust_rec.order_ids[3] = 2194
    LET obj = util.JSONObject.fromFGL(cust_rec)
    DISPLAY obj.toString()
END MAIN

String ==========>>>>>> JSON ==========>>>>>> FGL:

IMPORT util
MAIN
    DEFINE cust_rec RECORD
               cust_num INTEGER,
               cust_name VARCHAR(30),
               order_ids DYNAMIC ARRAY OF INTEGER
           END RECORD
    DEFINE js STRING
    DEFINE obj util.JSONObject
    LET js='{ "cust_num":2735, "cust_name":"McCarlson",
              "order_ids":[234,3456,24656,34561] }'
    LET obj = util.JSONObject.parse( js )
    CALL obj.toFGL( cust_rec )
    DISPLAY cust_rec.cust_name
    DISPLAY cust_rec.order_ids[4]
END MAIN

======遍历JSON取值======

            FOR i=1 TO l_json.getLength()
                    DISPLAY l_json.name(i),"",l_json.get(l_json.name(i)) 
            END FOR        

======修改、删除JSON的值======

IMPORT util
MAIN
    DEFINE obj util.JSONObject
    DEFINE rec RECORD
               id INTEGER,
               name STRING
           END RECORD
    DEFINE arr DYNAMIC ARRAY OF INTEGER
    LET obj = util.JSONObject.create()
    CALL obj.put("simple", 234)
    LET rec.id = 234
    LET rec.name = "Barton"
    CALL obj.put("record", rec)
    LET arr[1] = 234
    LET arr[2] = 2837
    CALL obj.put("array", arr)
    DISPLAY obj.toString()
END MAIN
IMPORT util
MAIN
    DEFINE obj util.JSONObject
    LET obj = util.JSONObject.create()
    CALL obj.put("address", "5 Brando Street")
    CALL obj.remove("address")
    DISPLAY obj.get("address")
END MAIN

以上来自Genero Studio Help帮助文档。

以下例子:

处理接口返回JSON内容:

#####l_str:{"state":1,"data":{"result":[{"contract_id":"TEST104352","contract_name":"采购合同","contract_code":"TEST104352","unit_name":"广东电网有限责任公司江门供电局","contacts_name":null,"contacts_phone":"135","contract_type":1,"project_code":"20200316","project_name":"采购项目","contract_amount":600000,"prepayment_ratio":20,"deposit_ratio":10,"payment_arrival_ratio":60,"bond_ratio":10,"purchase_id":"20200316","supply_duration":null,"bidding_proportion":null,"quota_allocation":null,"technical_agreement_number":null,"jssj":20200316,"qcsj":null,"rownum_":1}],"row_total":1,"page_size":100,"current_page":1},"msg":"成功","ext":{}}

注意:

[ ] 是数组格式,要用util.JSONArray接收处理;

{ } 是JSON格式,要用util.JSONObject接收处理;

PUBLIC FUNCTION cs_nfdw_get_contract_list2(p_str)
#处理获取到的合同信息列表
DEFINE P_STR                STRING
DEFINE json                 util.JSONObject
DEFINE i,j                  INT
DEFINE l_data               util.JSONObject
DEFINE l_result             util.JSONArray
DEFINE l_data2              util.JSONObject
    #
    LET json = util.JSONObject.parse(p_str)

    #返回多json获取
    LET l_data = json.get("data")
    DISPLAY "==================l_data:",l_data.toString()
    

    LET l_result = l_data.get("result")
    DISPLAY "==================l_result:",l_result.toString()
    

    FOR j=1 TO l_result.getLength()
        LET l_data2 = l_result.get(j)
        DISPLAY "==========",j,"========l_data2:",l_data2.toString()

        FOR i=1 TO l_data2.getLength()
            DISPLAY l_data2.name(i),"",l_data2.get(l_data2.name(i))
        END FOR
    END FOR
    

END FUNCTION

输出内容:

==================l_data:{"result":[{"contract_id":"TEST104352","contract_name":"采购合同","contract_code":"TEST104352","unit_name":"广东电网有限责任公司江门供电局","contacts_name":null,"contacts_phone":"135","contract_type":1,"project_code":"20200316","project_name":"采购项目","contract_amount":600000,"prepayment_ratio":20,"deposit_ratio":10,"payment_arrival_ratio":60,"bond_ratio":10,"purchase_id":"20200316","supply_duration":null,"bidding_proportion":null,"quota_allocation":null,"technical_agreement_number":null,"jssj":20200316,"qcsj":null,"rownum_":1}],"row_total":1,"page_size":100,"current_page":1}

==================l_result:[{"contract_id":"TEST104352","contract_name":"采购合同","contract_code":"TEST104352","unit_name":"广东电网有限责任公司江门供电局","contacts_name":null,"contacts_phone":"135","contract_type":1,"project_code":"20200316","project_name":"采购项目","contract_amount":600000,"prepayment_ratio":20,"deposit_ratio":10,"payment_arrival_ratio":60,"bond_ratio":10,"purchase_id":"20200316","supply_duration":null,"bidding_proportion":null,"quota_allocation":null,"technical_agreement_number":null,"jssj":20200316,"qcsj":null,"rownum_":1}]

==========          1========l_data2:{"contract_id":"TEST104352","contract_name":"采购合同","contract_code":"TEST104352","unit_name":"广东电网有限责任公司江门供电局","contacts_name":null,"contacts_phone":"135","contract_type":1,"project_code":"20200316","project_name":"采购项目","contract_amount":600000,"prepayment_ratio":20,"deposit_ratio":10,"payment_arrival_ratio":60,"bond_ratio":10,"purchase_id":"20200316","supply_duration":null,"bidding_proportion":null,"quota_allocation":null,"technical_agreement_number":null,"jssj":20200316,"qcsj":null,"rownum_":1}

contract_id:TEST104352
contract_name:采购合同
contract_code:TEST104352
unit_name:广东电网有限责任公司江门供电局
contacts_name:
contacts_phone:135
contract_type: 1.0
project_code:20200316
project_name:采购项目
contract_amount: 600000.0
prepayment_ratio: 20.0
deposit_ratio: 10.0
payment_arrival_ratio: 60.0
bond_ratio: 10.0
purchase_id:20200316
supply_duration:
bidding_proportion:
quota_allocation:
technical_agreement_number:
jssj: 20200316.0
qcsj:
rownum_: 1.0

原文地址:https://www.cnblogs.com/xiaoli9627/p/12660030.html