webspeed + json

rumi

Member
Hi guys,
is possible to return JSON from webspeed? i have a code like:

Code:
<script language="Speedscript">
  for each ad_mstr where ad_addr = "blalba" no-lock:
  {&OUT} ad_name ad_addr.
  end.
</script>

..so i would like to return javascript array(json) because of jquery autocomplete.

Thanks

rumi
 

atopher

New Member
You can use the WRITE-JSON command from a prodataset or TT, or manually build JSON from within your code - likely for jquery e.g.

Code:
{&out}
    '~{ "data": ['
    .
for each cust no-lock break by cust.acc:
{&out}
    '~{"name:":"' cust.name '"}'
    .
    if not last(cust.acc) then {&out} ','.
end.
{&out}
    ']}'
    .
http://www.jsonlint.com/ is very useful to validate the JSON returned.
 

rumi

Member
Hi, thank it work qood ... but i have another 2 problems -
... first problem is: i have a code:
Code:
           <script language="Speedscript">
              DEFINE VARIABLE retez AS CHARACTER.
 
              PROCEDURE output-header :
                RUN outputContentType IN web-utilities-hdl ("application/json":U).                
              END PROCEDURE. 
 
 
              DEFINE VARIABLE qw AS CHARACTER.
              qw = STRING(GET-VALUE("q")).
              qw = qw + '*'.  
 
              {&out} '~{"dt":['.
              FOR EACH si_mstr WHERE si_db = 'PC' AND si_site <> '6301' AND si_site <> '6302' AND si_site <> '6303' AND si_site <> '6101' AND si_site <> '6901' AND si_site MATCHES qw NO-LOCK BREAK BY si_site:
 
                {&out} '~{"value":"' si_site '~",'.              
                {&out} '~"label":"' si_desc '~"}'.
                IF NOT LAST(si_site) THEN {&out} ','.
              END.              
 
              {&out} ']}'.  
 
            </script>
and this code returns bad charset!(i would like to have a utf-8)
... and second problem is :
i have a code(above) in one file(test.r) and i call it in file caller.r - i use code -
Code:
$.ajax({
                       url: "test.r",
                       dataType: "json",
 
                        success: function( data ) {
                        response( $.map( data.dt, function( item ) {
                    return {
                            label: item.label ,
                            value: item.value
                           }
                  }));
                 }
                });
and i use it as a source for autocomplete jquery plugin - it works only in chrome - but it doesnt works in Explorer ... where is problem?

Thanks

rumi
 

Cecil

19+ years progress programming and still learning.
The UTF-8 problem and can be easily fixed by changing the "Agent startup parameters" setting under the WebSpeed's Agents properties. Just include -cpstream UTF-8 & -cpinternal UTF-8 . I'am also guessing that your database is UTF-8?

cpstreamutf-8.png

Your second problem regarding IE, might because of the JSON String. I found that I had to have all string values in Double Quotes. i.e. {"name":"John Smith"} also the values have to be trimmed (white spaced removed).

Also and you have to re-encode some charters because they are reserved within JSON here is my JSON-Encode funtion:

Code:
JSON-Encode RETURNS LONGCHAR
    (INPUT plcString AS LONGCHAR):
/*------------------------------------------------------------------------------
  Purpose:  
    Notes:  
------------------------------------------------------------------------------*/
    /* TODO: Better search & replace is needed, rather than a brut-force replace method.*/

    ASSIGN
        plcString = REPLACE(plcString, '~\','~\~\')    /* DO NOT CHANGE THE ORDER THIS HAS TO BE DONE FIRST!*/
        plcString = REPLACE(plcString, '"','~\"')
        plcString = REPLACE(plcString, '/','~\/')
        plcString = REPLACE(plcString, CHR(8),'~\b') /*backspace*/
        plcString = REPLACE(plcString, CHR(12),'~\f') /* Formfeed */
        plcString = REPLACE(plcString, CHR(10),'~\n') /*newline*/
        plcString = REPLACE(plcString, CHR(13),'~\r') /*carage return*/
        plcString = REPLACE(plcString, CHR(9),'~\t'). /*tab*/
        
    RETURN plcString.

END FUNCTION.

Hopes this helps you out.
 

atopher

New Member
IE is also particularly sensitive to extra commas in the JSON.

It would be worth installing firebug for Firefox (you can also use it with IE) and you'll be able to debug the AJAX calls as they are executed.
 

rumi

Member
hi, thanks for help - cecil: json function doesnt work - its output isnt valid json... i try to call thru XMLHttpRequest() - it works in chrome + firefox .... but not in Explorer .... i dont know where is problem
 

rumi

Member
hi,
i have a solution for my problem - problem was a bad charset - our czech letters broke my jSON format

rumi
 

webguy

Member
If you are using the jqueryui autocomplete, the json output needs the brackets.

Code:
 /* the input value name that is passed is called "term". */
assign v-search-txt = get-value("term");
 
 /* query */
 for each widget no-lock where
         blah blah...
 
       if v-count gt 0 then
       {&out} '[' skip.
 
       {&out} '~{' skip
                  '"label": "' + widget.name + '",' skip
                  '"value": "' + v-search-txt + '"' skip
       {&out} '~},' skip.
 
     if v-count ge 30 then leave.
 end.
 
 if v-count gt 0 then
    {&out} ']' skip.
 
Top