[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: When you ask a webservice in JSON, what do you probably get back ?



Who says there is no way to read JSON on the i? Ever looked at
http://rpgnextgen.com/index.php?content=json ?

Example:

http://rpgnextgen.com/downloads/json_par01.rpgle

     /**
      * \brief JSON Parser example
      *
      * Parses a JSON string and generates from the JSON object a new
      * JSON string.
      *
      * <br><br>
      *
      * The parser is quite tolerant. It accepts = instead of : . Quotes
can be
      * either single or double quotes. ; is also accepted instead of ,
.
      *
      * <br><br>
      *
      * <code>
      * { "Product number" : 186189 ,
      *   "Product description": "After Eight 200gr" ,
      *   "In stock" : 52455 ; ' +
      *   "Distribution lock" : False ,
      *   "Cost" = 1.235 ,
      *   "Supplier" = 358 ,
      *   "Category" = { "Id" : 15 , "Description": "Sweets"},
      *   "Product line" : [
      *       {"Sweets" = "120.1.1." } ,
      *       {"Chocolate" = "120.1.2" } ] ,
      *   "Assigned stocks" = [ ] ,
      *   "Assigned display" = {}
      *   }
      * </code>
      *
      * \author Mihael Schmidt
      * \date   13.03.2009
      */

     H dftactgrp(*no) actgrp(*caller)
     H bnddir('QC2LE')


 
*-----------------------------------------------------------------------
-
      * Prototypes
 
*-----------------------------------------------------------------------
-
      /copy JSON_H
      /copy JSON_C


 
*-----------------------------------------------------------------------
-
      * Variables
 
*-----------------------------------------------------------------------
-
     D json            S               *
     D string          S          65535A
     D json_string     S          65535A
      /free
       string = '{ "Product number" : 186189 , ' +
                  '"Product description": "After Eight 200gr" , ' +
                  '"In stock" : 52455 ; ' +
                  '"Distribution lock" : False , ' +
                  '"Cost" = 1.235 , ' +
                  '"Supplier" = 358 , ' +
                  '"Category" = { "Id" : 15 , "Description": "Sweets"},
' +
                  '"Product line" : [ ' +
                     '{"Sweets" = "120.1.1." } , ' +
                     '{"Chocolate" = "120.1.2" } ' +
                   '] , ' +
                  '"Assigned stocks" = [ ] ,' +
                  '"Assigned display" = {}  ' +
                  '} ' + x'00';

       json = json_parse(%addr(string));

       json_string = %str(json_toString(json));
       dsply %subst(json_string : 1 : 50);
       dsply %subst(json_string : 51 : 50);
       dsply %subst(json_string : 101 : 50);
       dsply %subst(json_string : 151 : 50);
       dsply %subst(json_string : 201 : 50);
       dsply %subst(json_string : 251 : 50);
       dsply %subst(json_string : 301 : 50);
       dsply %subst(json_string : 351 : 50);

       json_dispose(json);

       *inlr = *on;
       return;
      /end-free


 

-----Original Message-----
From: ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx
[mailto:ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of
hr@xxxxxxxxxxxx
Sent: Wednesday, October 14, 2009 10:30 PM
To: HTTPAPI and FTPAPI Projects
Subject: When you ask a webservice in JSON, what do you probably get
back ?

JSON ;-)

and there is no way to read JSON on an iSeries,
so I'm working on a new method that is able to
convert a recieved JSON object to a XML document
in the CGIDEV2 Responce Object ....

Input:
json = '{' 
 + 'data:[' 
 + '[select:"Y",display:"Y - Yes"]' 
 + '  ,[select:"N",display:"N - No"]' 
 + ' ] }'; 

Output:
<tree1 type="object"> 
 <data type="array"> 
  <tree3 type="array"> 
   <select>Y</select> 
   <display>Y - Yes</display> 
  </tree3> 
  <tree3 type="array"> 
   <select>N</select> 
   <display>N - No</display> 
  </tree3> 
 </data> 
</tree1> 

It's a little bit tricky because JSON comes back
in many formats and not always up to the standards
descibed.

But it can be done in rather few statements, and
here is a first shot that in the end will be packed
into PXAPICGI in a single method like

convertJSONtoXML(addr:length);
  buffaddr = bufAddr(); 
  buffsize = bufSize(); 
// do whatever you like 

First shot code:


 /free 
  clearSrvPgm(); 
  setContent('*none'); 
 
  json = '{' 
   + 'data:[' 
   + '[select:"Y",display:"Y - Yes"]' 
   + '  ,[select:"N",display:"N - No"]'
   + ' ] }'; 
 
  // packed in method 
  nodeDepth = 1; 
  nodeLength = 256; 
  for i = 1 to NodeLength; 
    cPrev = c; 
    c = %subst(json:i:1); 
    if nodeOpen = *off; 
      select; 
        when c = *blank; 
        when c = '"'; 
        when c = '{'; 
          nodeAttr = 'tree'+%char(nodeDepth);
          nodeArray(nodeDepth) = nodeAttr; 
          xmlNode(nodeAttr:'type="object"'); 
          nodeAttr = ''; 
          nodeDepth += 1; 
        when c = '}'; 
          nodeDepth -= 1; 
          xmlEndNode(); 
        when c = '['; 
          nodeAttr = 'tree'+%char(nodeDepth);
          nodeArray(nodeDepth) = nodeAttr; 
          xmlNode(nodeAttr:'type="array"'); 
          nodeAttr = ''; 
          nodeDepth += 1; 
        when c = ']'; 
          nodeDepth -= 1; 
          xmlEndNode(); 
        when c = ':'; 
          nodeOpen = *on; 
        when c = ','; 
        other; 
          nodeAttr += c; 
      endsl; 
 
    else; 
 
      select; 
        when nodeOpenC = *on; 
          select; 
            when c = '"' and cPrev = '\'; 
              nodeData += c; 
            when c = '\' and cPrev = '\'; 
              nodeData += c; 
            when c = '"' 
             or c = ' ' and nodeOpenB = *on 
             or c = ',' and nodeOpenB = *on; 
              xmlNode(nodeAttr:'':nodeData); 
              nodeAttr = ''; 
              nodeData = ''; 
              nodeOpen = *off; 
              nodeOpenC = *off; 
              nodeOpenB = *off; 
            other; 
              nodeData += c; 
          endsl; 
        when c = *blank; 
        when c = '"'; 
          nodeOpenC = *on; 
        // number, true, false, null 
        when c >= '0' and c <= '9' 
         or c = '-' 
         or c = '.' 
         or c = 't' 
         or c = 'f' 
         or c = 'n'; 
          nodeData += c; 
          nodeOpenC = *on; 
          nodeOpenB = *on; 
        when c = '{'; 
          nodeArray(nodeDepth) = nodeAttr; 
          xmlNode(nodeAttr:'type="object"');
          nodeAttr = ''; 
          nodeDepth += 1; 
          nodeOpen = *off; 
        when c = '}'; 
          nodeDepth -= 1; 
          xmlEndNode(); 
        when c = '['; 
          nodeArray(nodeDepth) = nodeAttr; 
          xmlNode(nodeAttr:'type="array"');
          nodeAttr = ''; 
          nodeDepth += 1; 
          nodeOpen = *off; 
        when c = ']'; 
          nodeDepth -= 1; 
          xmlEndNode(); 
        when c = ':'; 
        when c = ','; 
        other; 
      endsl; 
    endif; 
  endfor; 
 
  // back on the outside again 
  buffaddr = bufAddr(); 
  buffsize = bufSize(); 
  dsply result; 
  echoToStmf('/json.xml':1252);
  *inlr = *on; 
  return; 
 /end-free 
-----------------------------------------------------------------------
This is the FTPAPI mailing list.  To unsubscribe, please go to:
http://www.scottklement.com/mailman/listinfo/ftpapi
-----------------------------------------------------------------------