Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. {
  2. "John": {
  3. "status":"Wait"
  4. },
  5. "Jennifer": {
  6. "status":"Active"
  7. },
  8. "James": {
  9. "status":"Active",
  10. "age":56,
  11. "count":10,
  12. "progress":0.0029857,
  13. "bad":0
  14. }
  15. }
  16.  
  17. <?php
  18.  
  19. $string = file_get_contents("/home/michael/test.json");
  20. $json_a = json_decode($string, true);
  21.  
  22. echo $json_a['John'][status];
  23. echo $json_a['Jennifer'][status];
  24.  
  25. $jsonIterator = new RecursiveIteratorIterator(
  26. new RecursiveArrayIterator(json_decode($json, TRUE)),
  27. RecursiveIteratorIterator::SELF_FIRST);
  28.  
  29. foreach ($jsonIterator as $key => $val) {
  30. if(is_array($val)) {
  31. echo "$key:n";
  32. } else {
  33. echo "$key => $valn";
  34. }
  35. }
  36.  
  37. John:
  38. status => Wait
  39. Jennifer:
  40. status => Active
  41. James:
  42. status => Active
  43. age => 56
  44. count => 10
  45. progress => 0.0029857
  46. bad => 0
  47.  
  48. foreach ($json_a as $k => $v) {
  49. echo $k, ' : ', $v;
  50. }
  51.  
  52. <?php
  53.  
  54. $string = file_get_contents("/home/michael/test.json");
  55. $json_a = json_decode($string, true);
  56.  
  57. foreach ($json_a as $person_name => $person_a) {
  58. echo $person_a['status'];
  59. }
  60.  
  61. ?>
  62.  
  63. $shipments = json_decode(file_get_contents("shipments.js"), true);
  64. print_r($shipments);
  65.  
  66. $shipments = json_encode(json_decode(file_get_contents("shipments.js"), true));
  67. echo $shipments;
  68.  
  69. <?php
  70. $string = file_get_contents("/home/michael/test.json");
  71. $json_a=json_decode($string,true);
  72.  
  73. foreach ($json_a as $key => $value){
  74. echo $key . ':' . $value;
  75. }
  76. ?>
  77.  
  78. [ // <-- Note that I changed this
  79. {
  80. "name" : "john", // And moved the name here.
  81. "status":"Wait"
  82. },
  83. {
  84. "name" : "Jennifer",
  85. "status":"Active"
  86. },
  87. {
  88. "name" : "James",
  89. "status":"Active",
  90. "age":56,
  91. "count":10,
  92. "progress":0.0029857,
  93. "bad":0
  94. }
  95. ] // <-- And this.
  96.  
  97. $json_data = '{
  98. "John": {
  99. "status":"Wait"
  100. },
  101. "Jennifer": {
  102. "status":"Active"
  103. },
  104. "James": {
  105. "status":"Active",
  106. "age":56,
  107. "count":10,
  108. "progress":0.0029857,
  109. "bad":0
  110. }
  111. }';
  112.  
  113. $decode_data = json_decode($json_data);
  114. foreach($decode_data as $key=>$value){
  115.  
  116. print_r($value);
  117. }
  118.  
  119. $string = file_get_contents("/home/michael/test.json");
  120. $json = json_decode($string, true);
  121.  
  122. foreach ($json as $key => $value) {
  123. if (!is_array($value)) {
  124. echo $key . '=>' . $value . '<br />';
  125. } else {
  126. foreach ($value as $key => $val) {
  127. echo $key . '=>' . $val . '<br />';
  128. }
  129. }
  130. }
  131.  
  132. $jsondata = file_get_contents(PATH_TO_JSON_FILE."/jsonfile.json");
  133.  
  134. $array = json_decode($jsondata,true);
  135.  
  136. foreach($array as $k=>$val):
  137. echo '<b>Name: '.$k.'</b></br>';
  138. $keys = array_keys($val);
  139. foreach($keys as $key):
  140. echo '&nbsp;'.ucfirst($key).' = '.$val[$key].'</br>';
  141. endforeach;
  142. endforeach;
  143.  
  144. Name: John
  145. Status = Wait
  146. Name: Jennifer
  147. Status = Active
  148. Name: James
  149. Status = Active
  150. Age = 56
  151. Count = 10
  152. Progress = 0.0029857
  153. Bad = 0
  154.  
  155. foreach($json_a as $key => $value) {
  156. echo $key;
  157. if (gettype($value) == "object") {
  158. foreach ($value as $key => $value) {
  159. # and so on
  160. }
  161. }
  162. }
  163.  
  164. <?php
  165. $json = '{
  166. "response": {
  167. "data": [{"identifier": "Be Soft Drinker, Inc.", "entityName": "BusinessPartner"}],
  168. "status": 0,
  169. "totalRows": 83,
  170. "startRow": 0,
  171. "endRow": 82
  172. }
  173. }';
  174. $json = json_decode($json, true);
  175. //echo '<pre>'; print_r($json); exit;
  176. echo $json['response']['data'][0]['identifier'];
  177. $json['response']['data'][0]['entityName']
  178. echo $json['response']['status'];
  179. echo $json['response']['totalRows'];
  180. echo $json['response']['startRow'];
  181. echo $json['response']['endRow'];
  182.  
  183. ?>
  184.  
  185. foreach ($json_a as $key => $value)
  186. {
  187. echo $key, ' : ';
  188. foreach($value as $v)
  189. {
  190. echo $v." ";
  191. }
  192. }
  193.  
  194. <?php
  195. $json = file_get_contents('/home/michael/test.json');
  196. $json_a = json_decode($json);
  197. var_dump($json_a); // just to see the structure. It will help you for future cases
  198. echo "n";
  199. foreach($json_a as $row){
  200. echo $row->status;
  201. echo "n";
  202. }
  203. ?>
  204.  
  205. $json_a = json_decode($string, TRUE);
  206. $json_o = json_decode($string);
  207.  
  208.  
  209.  
  210. foreach($json_a as $person => $value)
  211. {
  212. foreach($value as $key => $personal)
  213. {
  214. echo $person. " with ".$key . " is ".$personal;
  215. echo "<br>";
  216. }
  217.  
  218. }
  219.  
  220. foreach($data as $object) {
  221.  
  222. foreach($object as $value) {
  223.  
  224. echo $value;
  225.  
  226. }
  227.  
  228. }
  229.  
  230. echo $json_a['John']['status'];
  231.  
  232. echo "<>"
  233.  
  234. echo $json_a['Jennifer']['status'];
  235.  
  236. br inside <>
  237.  
  238. wait
  239. active
  240.  
  241. <?php
  242. function jsonDecode1($json){
  243. $arr = json_decode($json, true);
  244. return $arr;
  245. }
  246.  
  247. // In case of malformed JSON, it will return NULL
  248. var_dump( jsonDecode1($json) );
  249. ?>
  250.  
  251. <?php
  252. function jsonDecode2($json){
  253. $arr = (array) json_decode($json, true);
  254. return $arr;
  255. }
  256.  
  257. // In case of malformed JSON, it will return an empty array()
  258. var_dump( jsonDecode2($json) );
  259. ?>
  260.  
  261. <?php
  262. function jsonDecode3($json){
  263. $arr = (array) json_decode($json, true);
  264.  
  265. if(empty(json_last_error())){
  266. return $arr;
  267. }
  268. else{
  269. throw new ErrorException( json_last_error_msg() );
  270. }
  271. }
  272.  
  273. // In case of malformed JSON, Fatal error will be generated
  274. var_dump( jsonDecode3($json) );
  275. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement