[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 ?



Did you see Mihael's open source project that includes support for 
parsing JSON?  It was posted on this mailing list 2 days ago:
http://www.scottklement.com/archives/ftpapi/200910/msg00064.html


hr@xxxxxxxxxxxx wrote:
>    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
> -----------------------------------------------------------------------

-----------------------------------------------------------------------
This is the FTPAPI mailing list.  To unsubscribe, please go to:
http://www.scottklement.com/mailman/listinfo/ftpapi
-----------------------------------------------------------------------