How to decode json of this kind?

[{"News": { "title": "\u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435", "content": "\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435" } }] 

My code is not working

 <?php $content = 'news'; // Π£ΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ Ρ‡Ρ‚ΠΎ Π½Π°ΠΌ Π½ΡƒΠΆΠ½Ρ‹ ΠΈΠΌΠ΅Π½Π½ΠΎ новости include_once('json.php'); // ЗапускаСм скрипт json'Π° $content_decode = json_decode($list1); // Π”Π΅ΠΊΠΎΠ΄ΠΈΡ€ΡƒΠ΅ΠΌ json foreach($content_decode->News as $text) { $title = $text->title; $content = $text->content; $main_content .= ' <div id="title"><div id="title_text">'.$title.'</div></div> <div id="content"><div id="content_text">'.$content.'</div></div>'; } echo '<html> <head> <title>W_0rld - Новости</title> <meta charset="utf-8"> <link href="../style/ui.css" rel="stylesheet"> <script>var limit = 10;</script> </head> <body onload="onload();"> <div id="menu"> <div id="logo" style=>W_0rld</div> <div id="links"> <a class="news" href="/" id="link">Новости</a> <a class="list1" href="/page/1" id="link">Услуги</a> <a class="list2" href="/page/2" id="link">Π—Π°ΠΊΠ°Π·Π°Ρ‚ΡŒ</a> <a class="list3" href="/page/3" id="link">Обо ΠΌΠ½Π΅</a> <a href="//vk.com/W_0rld" id="link">Π’Πš</a> </div> </div> <div id="container">'.$main_content.'</div> <div id="footer" style="display:none;">W_0rld - Новости</div> </body></html>'; 
  • one
    you are trying to use foreach on an object. News is not an array. - etki
  • There were no problems with decoding, but the problem indicated by Etki was reproduced. - cheops

3 answers 3

 $json = '{ "News": [ { "title": "\u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435", "content": "\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435" }, { "title": "Head", "content": "Content" } ] }'; $json = json_decode($json); foreach($json->News as $new) { echo $new->title.', '.$new->content.'<br>'; } 

НазваниС, Π‘ΠΎΠ΄Π΅Ρ€ΠΆΠ°Π½ΠΈΠ΅ Head, Content


 $news = array("News" => array( array("title" => "Head", "content" => "Content") )); $json = json_encode($news); 

var_dump($json); string(47) "{"News":[{"title":"Head","content":"Content"}]}"

 string(47) "{ "News": [ { "title": "Head", "content": "Content" } ] }" 
  • I understood what my problem is: my json is not valid. What needs to be changed so that it becomes valid? Here is my code: w-0rld.ru/download/1.txt - W_0rld
  • @ W_0rld, added encode in response - Mr. Black
 ?php $content = 'news'; // Π£ΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ Ρ‡Ρ‚ΠΎ Π½Π°ΠΌ Π½ΡƒΠΆΠ½Ρ‹ ΠΈΠΌΠ΅Π½Π½ΠΎ новости include_once('json.php'); // ЗапускаСм скрипт json'Π° $content_decode = json_decode($list1); // Π”Π΅ΠΊΠΎΠ΄ΠΈΡ€ΡƒΠ΅ΠΌ json foreach($content_decode as $text) { $title = $text['title']; $content = $text['content']; $main_content .= ' <div id="title"><div id="title_text">'.$title.'</div></div> <div id="content"><div id="content_text">'.$content.'</div></div>'; } $content_decode - это массив Π° Π½Π΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ 
  • In this case, $content_decode is just an object. To convert to the associative array json_decode($json, true); - Mr. Black

just add the second parameter true to json_decode($json,true) and the function will always return an array.