Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "John": {
- "status":"Wait"
- },
- "Jennifer": {
- "status":"Active"
- },
- "James": {
- "status":"Active",
- "age":56,
- "count":10,
- "progress":0.0029857,
- "bad":0
- }
- }
- <?php
- $string = file_get_contents("/home/michael/test.json");
- $json_a = json_decode($string, true);
- echo $json_a['John'][status];
- echo $json_a['Jennifer'][status];
- $jsonIterator = new RecursiveIteratorIterator(
- new RecursiveArrayIterator(json_decode($json, TRUE)),
- RecursiveIteratorIterator::SELF_FIRST);
- foreach ($jsonIterator as $key => $val) {
- if(is_array($val)) {
- echo "$key:n";
- } else {
- echo "$key => $valn";
- }
- }
- John:
- status => Wait
- Jennifer:
- status => Active
- James:
- status => Active
- age => 56
- count => 10
- progress => 0.0029857
- bad => 0
- foreach ($json_a as $k => $v) {
- echo $k, ' : ', $v;
- }
- <?php
- $string = file_get_contents("/home/michael/test.json");
- $json_a = json_decode($string, true);
- foreach ($json_a as $person_name => $person_a) {
- echo $person_a['status'];
- }
- ?>
- $shipments = json_decode(file_get_contents("shipments.js"), true);
- print_r($shipments);
- $shipments = json_encode(json_decode(file_get_contents("shipments.js"), true));
- echo $shipments;
- <?php
- $string = file_get_contents("/home/michael/test.json");
- $json_a=json_decode($string,true);
- foreach ($json_a as $key => $value){
- echo $key . ':' . $value;
- }
- ?>
- [ // <-- Note that I changed this
- {
- "name" : "john", // And moved the name here.
- "status":"Wait"
- },
- {
- "name" : "Jennifer",
- "status":"Active"
- },
- {
- "name" : "James",
- "status":"Active",
- "age":56,
- "count":10,
- "progress":0.0029857,
- "bad":0
- }
- ] // <-- And this.
- $json_data = '{
- "John": {
- "status":"Wait"
- },
- "Jennifer": {
- "status":"Active"
- },
- "James": {
- "status":"Active",
- "age":56,
- "count":10,
- "progress":0.0029857,
- "bad":0
- }
- }';
- $decode_data = json_decode($json_data);
- foreach($decode_data as $key=>$value){
- print_r($value);
- }
- $string = file_get_contents("/home/michael/test.json");
- $json = json_decode($string, true);
- foreach ($json as $key => $value) {
- if (!is_array($value)) {
- echo $key . '=>' . $value . '<br />';
- } else {
- foreach ($value as $key => $val) {
- echo $key . '=>' . $val . '<br />';
- }
- }
- }
- $jsondata = file_get_contents(PATH_TO_JSON_FILE."/jsonfile.json");
- $array = json_decode($jsondata,true);
- foreach($array as $k=>$val):
- echo '<b>Name: '.$k.'</b></br>';
- $keys = array_keys($val);
- foreach($keys as $key):
- echo ' '.ucfirst($key).' = '.$val[$key].'</br>';
- endforeach;
- endforeach;
- Name: John
- Status = Wait
- Name: Jennifer
- Status = Active
- Name: James
- Status = Active
- Age = 56
- Count = 10
- Progress = 0.0029857
- Bad = 0
- foreach($json_a as $key => $value) {
- echo $key;
- if (gettype($value) == "object") {
- foreach ($value as $key => $value) {
- # and so on
- }
- }
- }
- <?php
- $json = '{
- "response": {
- "data": [{"identifier": "Be Soft Drinker, Inc.", "entityName": "BusinessPartner"}],
- "status": 0,
- "totalRows": 83,
- "startRow": 0,
- "endRow": 82
- }
- }';
- $json = json_decode($json, true);
- //echo '<pre>'; print_r($json); exit;
- echo $json['response']['data'][0]['identifier'];
- $json['response']['data'][0]['entityName']
- echo $json['response']['status'];
- echo $json['response']['totalRows'];
- echo $json['response']['startRow'];
- echo $json['response']['endRow'];
- ?>
- foreach ($json_a as $key => $value)
- {
- echo $key, ' : ';
- foreach($value as $v)
- {
- echo $v." ";
- }
- }
- <?php
- $json = file_get_contents('/home/michael/test.json');
- $json_a = json_decode($json);
- var_dump($json_a); // just to see the structure. It will help you for future cases
- echo "n";
- foreach($json_a as $row){
- echo $row->status;
- echo "n";
- }
- ?>
- $json_a = json_decode($string, TRUE);
- $json_o = json_decode($string);
- foreach($json_a as $person => $value)
- {
- foreach($value as $key => $personal)
- {
- echo $person. " with ".$key . " is ".$personal;
- echo "<br>";
- }
- }
- foreach($data as $object) {
- foreach($object as $value) {
- echo $value;
- }
- }
- echo $json_a['John']['status'];
- echo "<>"
- echo $json_a['Jennifer']['status'];
- br inside <>
- wait
- active
- <?php
- function jsonDecode1($json){
- $arr = json_decode($json, true);
- return $arr;
- }
- // In case of malformed JSON, it will return NULL
- var_dump( jsonDecode1($json) );
- ?>
- <?php
- function jsonDecode2($json){
- $arr = (array) json_decode($json, true);
- return $arr;
- }
- // In case of malformed JSON, it will return an empty array()
- var_dump( jsonDecode2($json) );
- ?>
- <?php
- function jsonDecode3($json){
- $arr = (array) json_decode($json, true);
- if(empty(json_last_error())){
- return $arr;
- }
- else{
- throw new ErrorException( json_last_error_msg() );
- }
- }
- // In case of malformed JSON, Fatal error will be generated
- var_dump( jsonDecode3($json) );
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement