0

I have a big JSON object with lots of data in it. I want to print out a specific bit of data from it, but can't seem to find the right syntax for the print statement.

Here's the first bit of the json object so you can see the structure:

{"response":{"status":"OK","advertisers":[{"id":26243,"code":null,"name":"Tego","state":"inactive","default_brand_id":null,"remarketing_segment_id":null,"lifetime_budget":null,"lifetime_budget_imps":null,"daily_budget":null,"daily_budget_imps":null,"enable_pacing":null,"profile_id":508487,"control_pct":0,"timezone":"PST8PDT","last_modified":"2011-08-11 04:35:39","stats":null,"billing_internal_user":null,"billing_address1":"","billing_address2":"","billing_city":"","billing_state":"","billing_country":"","billing_zip":"","default_category":{"id":"5","name":"Computers and Electronics"},"default_currency":"USD","labels":[{"id":"1","name":"Salesperson","value":"Kurt Mackey"},{"id":"3","name":"Account Manager",}],"use_insertion_orders":false,"time_format":"12-hour","default_brand":null},

I want to return the ID of the first advertiser (which should be 26243).

I'm trying to print that out with this statement:

print $result->response->advertisers[0]->id

But it does not appear to be working. I get a blank white page.

What am I doing wrong?

4

2 に答える 2

1

You need to decode the string first with json_decode($jsonString). After that you will be able to access the different properties through the print statement you quoted.

$json = '{"response":{"status":"OK","advertisers":[{"id":26243,"code":null,"name":"Tego","state":"inactive","default_brand_id":null,"remarketing_segment_id":null,"lifetime_budget":null,"lifetime_budget_imps":null,"daily_budget":null,"daily_budget_imps":null,"enable_pacing":null,"profile_id":508487,"control_pct":0,"timezone":"PST8PDT","last_modified":"2011-08-11 04:35:39","stats":null,"billing_internal_user":null,"billing_address1":"","billing_address2":"","billing_city":"","billing_state":"","billing_country":"","billing_zip":"","default_category":{"id":"5","name":"Computers and Electronics"},"default_currency":"USD","labels":[{"id":"1","name":"Salesperson","value":"Kurt Mackey"},{"id":"3","name":"Account Manager",}],"use_insertion_orders":false,"time_format":"12-hour","default_brand":null}'
$jsonObj = json_decode($json);
print_r($jsonObj);
于 2012-05-01T09:53:48.753 に答える
0

You need to re-validate your json data. Because it is find for me after some modification in json data.

"labels":[{"id":"1","name":"Salesperson","value":"Kurt Mackey"},{"id":"3","name":"Account Manager",}]

see "labels1". it has no value as "labels[0]" has.

Try Json Schema PHP Validator

    <?php
$json = '{"response":{"status":"OK","advertisers":[{"id":26243,"code":null,"name":"Tego","state":"inactive","default_brand_id":null,"remarketing_segment_id":null,"lifetime_budget":null,"lifetime_budget_imps":null,"daily_budget":null,"daily_budget_imps":null,"enable_pacing":null,"profile_id":508487,"control_pct":0,"timezone":"PST8PDT","last_modified":"2011-08-11 04:35:39","stats":null,"billing_internal_user":null,"billing_address1":"","billing_address2":"","billing_city":"","billing_state":"","billing_country":"","billing_zip":"","default_category":{"id":"5","name":"Computers and Electronics"},"default_currency":"USD","labels":[{"id":"1","name":"Salesperson","value":"Kurt Mackey"},{"id":"3","name":"Account Manager","value":null}],"use_insertion_orders":false,"time_format":"12-hour","default_brand":null}]
}}';

$result = json_decode($json);
print $result->response->advertisers[0]->id;
于 2012-05-01T10:21:00.917 に答える