json help

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,
 
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.
 
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.webp

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.webp

:confused:
 
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.

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($contextUrl, 0, $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.
 
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.
 
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.
 
Top Bottom