1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

json help

Discussion in 'XenForo Development Discussions' started by MattW, May 15, 2013.

  1. MattW

    MattW Well-Known Member

    I'm doing a small add-on which is targeting an API that returns JSON

    I'm trying to get this to work in a template.

    If I dump the var out, I get the below response

    {xen:helper dump, $usage}

    Code:
    object(stdClass)#99 (6) {
      ["24h"] => string(4) "2 GB"
      ["48h"] => string(4) "4 GB"
      ["30d"] => string(5) "14 GB"
      ["02m"] => string(7) "0 Bytes"
      ["01m"] => string(7) "0 Bytes"
      ["00m"] => string(5) "14 GB"
    }

    and if I use the {xen:helper json, $usage} it returns:
    Code:
    {"24h":"2 GB","48h":"4 GB","30d":"14 GB","02m":"0 Bytes","01m":"0 Bytes","00m":"14 GB"}
    How do I actually go about formatting this for use in a template now?

    This is really easy in normal PHP as I just split the results in the foreach to separate variables

    PHP:
    $usage json_decode($resp);
     
    echo 
    "<table>";
    foreach(
    $usage as $k=>$v)
        echo 
    "<tr><td>$k</td><td>$v</td></tr>";
    echo 
    "</table>";
    Thanks,
     
    lsxforo likes this.
  2. Chris D

    Chris D XenForo Developer Staff Member

    Yeah the json helper actually encodes it into json so that won't work.

    Have you considered decoding the results in PHP and then passing that decoded results back to your template?

    If you use:

    $array = json_decode($json, true);

    The "true" statement will cast the JSON object into an associative array rather than an object which should be easier to work with.
     
    lsxforo and MattW like this.
  3. MattW

    MattW Well-Known Member

    Thanks Chris ;)

    What's the best way to then format the results of that array? I've tried doing it like this:

    Code:
    <div class="section">
        <div class="secondaryContent statsList" id="attachStats">
                    <h3>{xen:phrase attachment_statistics}</h3>
                    <div class="pairsJustified">
                        <dl>
                        <dt>{xen:phrase attachment_num_files}:</dt>
                        <dd>{xen:number $totalAttachments}</dd>
                        <dt>{xen:phrase attachment_disk_usage}:</dt>
                        <dd>{xen:number $sizeAttachments, 2}</dd>
                        </dl>
                    </div>
            </div>
        </div>
    Which is how I did it on the attachment stats add-on, which works:
    cdn.PNG

    but as soon as I change a phrase, it breaks

    Code:
    {xen:helper dump, $usage}
    <div class="section">
        <div class="secondaryContent statsList" id="attachStats">
                    <h3>{xen:phrase attachment_statistics}</h3>
                    <div class="pairsJustified">
                        <dl>
                        <dt>{xen:phrase cdn77_24h}:</dt>
                        <dd>{$usage.24h}</dd>
                        <dt>{xen:phrase attachment_disk_usage}:</dt>
                        <dd>{$usage.48h}</dd>
                        </dl>
                    </div>
            </div>
        </div>
    cdn2.PNG

    :confused:
     
  4. MattW

    MattW Well-Known Member

    Managed to sort it ;)
     
  5. lsxforo

    lsxforo Member

    Hello @Chris D, I have an additional question relative this.
    For me, it is problem to parse BbCode.

    1.
    In this case, getting data from API contains when runs query with keywords. And then followed your json_decode($jsondata, true), still remains <b> </b> in there title or its description.
    To solve it I try to get rid of tags using
    PHP:
      // $formatter = XenForo_BbCode_Formatter_Base::create('Base');
      // $parser = XenForo_BbCode_Parser::create($formatter);
      // $items = $parser->render($jsonData);
    My trial is like;
    PHP:
      $contextUrl 'https://apis.daum.net/search/web?apikey='.$daumAPIkey.'&q='.$searchKeyword.'&output=json';

      
    //fetch the data through webserver using the Host http header we set
      //https://xenforo.com/community/threads/json-help.50376/#post-538903 by Chris D.
      
    $responseData json_decode(file_get_contents($contextUrl0$request), true);

    // Zend_Debug::dump($responseData);

      
    $formatter XenForo_BbCode_Formatter_Base::create('Base');
      
    $parser XenForo_BbCode_Parser::create($formatter);
      
    $items $parser->render($responseData);

      
    $response->params['items'] = $items;
      
    $response->templateName 'hh_test';
    But, I didn't parse or BbCode Parse while render it. Otherwise others works very well except <b> tags.
    What is wrong?
    I leave thank you.
     
  6. Chris D

    Chris D XenForo Developer Staff Member

    json_decode will either return an object or an array (it returns an array if the second argument is true).

    The parser's render method would expect a string.
     
    lsxforo likes this.
  7. lsxforo

    lsxforo Member

    (n) What kind of string do you mean?
    PHP:
    $items $parser->render($responseData$array());
     
  8. Chris D

    Chris D XenForo Developer Staff Member

    Well the parser accepts a string like:

    [b]This will be bold[/b]

    And converts it to HTML like this:

    <b>This will be bold</b>

    Therefore whatever you pass in to the render method should be a string of text you want to be converted from BB code to HTML.

    You're passing in an array.
     

Share This Page