Jump to content

Dealing with JSON?


simonp

Recommended Posts

Hi,

 

I have a script that outputs:

 

[{"domain":"testdomain","tld":".net","result":"Taken"},{"domain":"testdomain","tld":".com","result":"Taken"},{"domain":"testdomain","tld":".mobi","result":"Taken"},{"domain":"testdomain","tld":".biz","result":"Taken"},{"domain":"testdomain","tld":".info","result":"Taken"}]

 

(or similar).

 

I believe this is JSON (which I've not used before!) which I understand is just like XML - human-readable results of a query.

 

So I now need to present this in a nice way in a web page. Is there something special in PHP I can use to do this?

 

Hope someone can help!

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/192672-dealing-with-json/
Share on other sites

Cheers - I can use json_decode which gives me an array:

 

Array ( [0] => Array ( [domain] => testdomain [tld] => .net [result] => Taken ) [1] => Array ( [domain] => testdomain [tld] => .com [result] => Taken ) [2] => Array ( [domain] => testdomain [tld] => .mobi [result] => Taken ) [3] => Array ( [domain] => testdomain [tld] => .biz [result] => Taken ) [4] => Array ( [domain] => testdomain [tld] => .info [result] => Taken ) ) 

 

But how do I display that nicely on a page? I think I need to use something like foreach but I've always been confused by arrays.

 

Any help welcome!

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/192672-dealing-with-json/#findComment-1015046
Share on other sites

Here's a loop which outputs some of those values, it should give you an idea of what to do.

 

$json = '[{"domain":"testdomain","tld":".net","result":"Taken"},{"domain":"testdomain","tld":".com","result":"Taken"},{"domain":"testdomain","tld":".mobi","result":"Taken"},{"domain":"testdomain","tld":".biz","result":"Taken"},{"domain":"testdomain","tld":".info","result":"Taken"}]';
$data = json_decode($json, TRUE);

foreach ($data as $domain) {
    echo $domain['domain'] . $domain['tld'] . ' is ' . $domain['result'] . '<br>';
}

Link to comment
https://forums.phpfreaks.com/topic/192672-dealing-with-json/#findComment-1015050
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.