简体   繁体   English

在PHP中打印出一个大json对象的特定位?

[英]Printing out specific bit of a big json object in PHP?

I have a big JSON object with lots of data in it. 我有一个很大的JSON对象,里面有很多数据。 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: 这是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},

I want to return the ID of the first advertiser (which should be 26243). 我想返回第一个广告客户的ID(应该为26243)。

I'm trying to print that out with this statement: 我正在尝试使用以下语句将其打印出来:

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

But it does not appear to be working. 但是它似乎没有用。 I get a blank white page. 我得到空白页。

What am I doing wrong? 我究竟做错了什么?

You need to decode the string first with json_decode($jsonString) . 您需要先使用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);

You need to re-validate your json data. 您需要重新验证您的json数据。 Because it is find for me after some modification in json data. 因为它是在对json数据进行一些修改后为我找到的。

"labels":[{"id":"1","name":"Salesperson","value":"Kurt Mackey"},{"id":"3","name":"Account Manager",}] “ labels”:[{“ id”:“ 1”,“名称”:“销售员”,“值”:“ Kurt Mackey”},{“ id”:“ 3”,“名称”:“客户经理”, }]

see "labels 1 ". 请参阅“标签1 ”。 it has no value as "labels[0]" has. 它没有“ labels [0]”具有的值。

Try Json Schema PHP Validator 试试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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM