Jump to content

How do I get this variable from JSON?!


simonp

Recommended Posts

Hi,

 

I've got this JSON output:

{"result":"success","clientid":null,"serviceid":null,"pid":null,"domain":"testdomain.com","totalresults":"1","startnumber":0,"numreturned":1,"products":{"product":[{"id":"3804","clientid":"1401","orderid":"3276","pid":"53","regdate":"2010-02-14","name":"Basic","groupname":"Plans","domain":"testdomain.com","dedicatedip":"","serverid":"4","servername":"serverx.com","serverip":"123.123.123.123","serverhostname":"","firstpaymentamount":"7.99","recurringamount":"7.99","paymentmethod":"wibble","paymentmethodname":"Credit\/Debit Card","billingcycle":"Monthly","nextduedate":"2010-02-14","status":"Terminated","username":"dcdqw","password":"z1nmY1p4Y9","subscriptionid":"","promoid":"0","overideautosuspend":"","overidesuspenduntil":"0000-00-00","ns1":"","ns2":"","assignedips":"","notes":"","diskusage":"0","disklimit":"1000","bwusage":"0","bwlimit":"50000","lastupdate":"2015-05-01 06:01:17","customfields":{"customfield":[{"id":"36","name":"ThisUserID","value":"1243214"}]},"configoptions":{"configoption":[]}}]}}

... and I'm trying to get the value for ThisUserID but I'm struggling so much with the arrays!

 

I've copied it into http://jsonviewer.stack.hu so I can understand it more but just can't get that value.

 

I understand:

$clientid = $arr->clientid;

... but can't work out how to drill deeper.

 

Any help appreciated ;)

Link to comment
https://forums.phpfreaks.com/topic/296084-how-do-i-get-this-variable-from-json/
Share on other sites

->clientid is null so that's not it. [edit] Oh, the general syntax? Yes, if you decode JSON to an object (the default) then you'd use lots of ->s. [/edit]

 

Starting at ThisUserID and working backwards,

- That's the value and the key is "name"

- "name" is part of an object in the "customfield" array

- "customfield" is an array in the "customfields" object

- "customfields" is part of an object in the "product" array

- "product" is an array in the "products" object

- "products" is at the top level

 

Reversing that gives you the path.

$arr->products->product[0]->customfields->customfield[0]->name
There are two arrays in there. You need to consider whether they will have any objects inside them at all, whether there is only ever one, or whether there could be more than one.

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.