util.JSONArray.put()

 

util.JSONArray.put() adds a new member to the specified JSON array.

 

Syntax:

 

util.JSONObject.put(index, value)

 

Parameters:

 

index

an index of the member of the specified JSON array

value

a value to be associated with this index

 

Usage and examples:

 

The member added by util.JSONArray.put() must be assigned to a variable of the util.JSONArray data type.

 

The index of the first array member is 1.

 

The value of the array member can be a simple string or numeric value as well as a complex value of RECORD or DYNAMIC ARRAY data type.

 

example code #1

(with simple string and

 numeric values)

MAIN

  DEFINE json_arr util.JSONArray

    LET json_arr = util.JSONArray.create()

    CALL json_arr.put(1, 123)

    CALL json_arr.put(2, "abc")

    DISPLAY json_arr.toString()  

  CALL fgl_getkey()

END MAIN

 

obtained results #1

 

example code #2

(with a JSON object)

MAIN

  DEFINE json_arr util.JSONArray

  DEFINE cust_rec RECORD

           cust_id INTEGER,

           cust_name STRING

         END RECORD

    

    LET json_arr = util.JSONArray.create()

    

    LET cust_rec.cust_id = 15

    LET cust_rec.cust_name = "Barton, Timothy"

    

    CALL json_arr.put(1, cust_rec)

    DISPLAY json_arr.toString()  

    

  CALL fgl_getkey()

END MAIN

 

obtained results #2

 

example code #3

(with a JSONArray)

MAIN

  DEFINE json_arr util.JSONArray

  DEFINE da DYNAMIC ARRAY OF INTEGER

    LET json_arr = util.JSONArray.create()

    LET da[1] = 1

    LET da[2] = 12

    LET da[3] = 123

    CALL json_arr.put(1, da)

    DISPLAY json_arr.toString()  

  CALL fgl_getkey()

END MAIN

 

obtained results #3

 

If a JSON array has a member with the specified index already, the value of member will be changed:

 

 

Array members can be returned with the util.JSONArray.get() method.

Array members can be removed with the util.JSONArray.remove() method.

 

 

Example programs:

CVS server: client.querix.com

CVS repository: /lycia_doc_examples

User: client

Project: auxiliary_features/json

Program: util_JSONArray_put

 

Related articles:

util.JSONArray.get()

util.JSONArray.remove()