Jump to content

How do I get this variable from JSON?!


simonp
Go to solution Solved by requinix,

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
Share on other sites

  • Solution

->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. Edited by requinix
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.