• 12-11-2013, 21:08:55
    #1
    Merhaba,
    arkadaşlar konunun daha net anlaşılması için bir görsel hazırladım ve olayı detaylı olarak anlattım.

    Amacım şu verileri listeletip başka bir dosya yardımı ile istediğimi okutup kullanma. Bunun için ise aşağıdaki formata uygun olması gerekiyor ama döngüye soktuğumda hatalar alıyorum.

    Bu işlemi yaparken kullandığım php kodlar:
    <?
      
      include "connect.php";
      $sql=mysql_query("select * from post");
      $data = array();
    while($row=mysql_fetch_array($sql)){ 
    
    	$kategori = array(
        	"status" => "1",
    		"category" => array(
        		"id" => 1,
    			"title" => "title",
    			"description"=>"",
    		),
    		"posts" => array(array(
    		
    		"id"=> 3771,
          	"type"=> "post",
          	"title"=> "gönderi başlığı",
          	"content"=> "içerik",
          	"excerpt"=> "short",
          	"date"=> "11.02.2013",
    	  	"comments"=> array(),
          	"com1"=> array(),
          	"com2"=> array("sd"=> 3771),
    			
    		))
    		
        	
    );
    
     }
    
    
    $json = json_encode($kategori);
    
    echo $json;
    	?>
  • 12-11-2013, 21:21:41
    #2
    Kimlik doğrulama veya yönetimden onay bekliyor.
    mantıken dizide aynı anahtar isimli iki eleman bulundurulamaz diye biliyorum? sizin yapınızdansa bu JSON formatı daha uygun olacaktır.

    <?php
    	
    	header("Content-Type: text/plain; charset=UTF-8");
    	
    	$results = (object) array(
    		"status" => true,
    		"category" => (object) array(
    			"ID" => 1,
    			"title" => "title",
    			"description" => "description"
    		),
    		"posts" => array(
    			(object) array(
    				"ID" => 1,
    				"type" => "post",
    				"title" => "title",
    				"content" => "content",
    				"excerpt" => "excerpt",
    				"published_at" => (time() - (72 * 60 * 60)),
    				"comments" => array()
    			),
    			(object) array(
    				"ID" => 2,
    				"type" => "post",
    				"title" => "title",
    				"content" => "content",
    				"excerpt" => "excerpt",
    				"published_at" => (time() - (24 * 60 * 60)),
    				"comments" => array()
    			)
    		)
    	);
    	
    	print_r(
    		json_encode(
    			$results,
    			JSON_PRETTY_PRINT
    		)
    	);
    {
        "status": true,
        "category": {
            "ID": 1,
            "title": "title",
            "description": "description"
        },
        "posts": [
            {
                "ID": 1,
                "type": "post",
                "title": "title",
                "content": "content",
                "excerpt": "excerpt",
                "published_at": 1384024870,
                "comments": [
    
                ]
            },
            {
                "ID": 2,
                "type": "post",
                "title": "title",
                "content": "content",
                "excerpt": "excerpt",
                "published_at": 1384197670,
                "comments": [
    
                ]
            }
        ]
    }
    Çıktıyı t6.json dosyasına kaydettikten sonra dosyadan okuyup örnek işlemler yapabiliriz.

    <?php
    	
    	header("Content-Type: text/plain; charset=UTF-8");
    	
    	$response = file_get_contents("t6.json");
    	
    	$results = json_decode($response);
    	
    	print_r($results);
    	
    	foreach($results->posts as $post) {
    		echo "\x20\n";
    		
    		print_r($post);
    		
    		echo "\x20\n";
    	}
    Örnek kullanımın çıktısı şu şekilde olacaktır.

    stdClass Object
    (
        [status] => 1
        [category] => stdClass Object
            (
                [ID] => 1
                [title] => title
                [description] => description
            )
    
        [posts] => Array
            (
                [0] => stdClass Object
                    (
                        [ID] => 1
                        [type] => post
                        [title] => title
                        [content] => content
                        [excerpt] => excerpt
                        [published_at] => 1384025003
                        [comments] => Array
                            (
                            )
    
                    )
    
                [1] => stdClass Object
                    (
                        [ID] => 2
                        [type] => post
                        [title] => title
                        [content] => content
                        [excerpt] => excerpt
                        [published_at] => 1384197803
                        [comments] => Array
                            (
                            )
    
                    )
    
            )
    
    )
     
    stdClass Object
    (
        [ID] => 1
        [type] => post
        [title] => title
        [content] => content
        [excerpt] => excerpt
        [published_at] => 1384025003
        [comments] => Array
            (
            )
    
    )
     
     
    stdClass Object
    (
        [ID] => 2
        [type] => post
        [title] => title
        [content] => content
        [excerpt] => excerpt
        [published_at] => 1384197803
        [comments] => Array
            (
            )
    
    )
  • 12-11-2013, 21:33:11
    #3
    Öncelikle ilgin için teşekkür ederim ama aşağıdaki kodu
    <?php 
         
        header("Content-Type: text/plain; charset=UTF-8"); 
         
        $results = (object) array( 
            "status" => true, 
            "category" => (object) array( 
                "ID" => 1, 
                "title" => "title", 
                "description" => "description" 
            ), 
            "posts" => array( 
                (object) array( 
                    "ID" => 1, 
                    "type" => "post", 
                    "title" => "title", 
                    "content" => "content", 
                    "excerpt" => "excerpt", 
                    "published_at" => (time() - (72 * 60 * 60)), 
                    "comments" => array() 
                ), 
                (object) array( 
                    "ID" => 2, 
                    "type" => "post", 
                    "title" => "title", 
                    "content" => "content", 
                    "excerpt" => "excerpt", 
                    "published_at" => (time() - (24 * 60 * 60)), 
                    "comments" => array() 
                ) 
            ) 
        ); 
         
        print_r( 
            json_encode( 
                $results, 
                JSON_PRETTY_PRINT 
            ) 
        );
    1.php eklediğimde ve tarayıcıdan 1.php çalıştırdığımda şu hatayı almaktayım

    <br />
    <b>Warning</b>: json_encode() expects parameter 2 to be long, string given in <b>/home/***/public_html/demo/maps/list.php</b> on line <b>38</b><br />
  • 12-11-2013, 21:43:17
    #4
    Kimlik doğrulama veya yönetimden onay bekliyor.
    RHX adlı üyeden alıntı: mesajı görüntüle
    Öncelikle ilgin için teşekkür ederim ama aşağıdaki kodu
    <?php 
         
        header("Content-Type: text/plain; charset=UTF-8"); 
         
        $results = (object) array( 
            "status" => true, 
            "category" => (object) array( 
                "ID" => 1, 
                "title" => "title", 
                "description" => "description" 
            ), 
            "posts" => array( 
                (object) array( 
                    "ID" => 1, 
                    "type" => "post", 
                    "title" => "title", 
                    "content" => "content", 
                    "excerpt" => "excerpt", 
                    "published_at" => (time() - (72 * 60 * 60)), 
                    "comments" => array() 
                ), 
                (object) array( 
                    "ID" => 2, 
                    "type" => "post", 
                    "title" => "title", 
                    "content" => "content", 
                    "excerpt" => "excerpt", 
                    "published_at" => (time() - (24 * 60 * 60)), 
                    "comments" => array() 
                ) 
            ) 
        ); 
         
        print_r( 
            json_encode( 
                $results, 
                JSON_PRETTY_PRINT 
            ) 
        );
    1.php eklediğimde ve tarayıcıdan 1.php çalıştırdığımda şu hatayı almaktayım

    <br />
    <b>Warning</b>: json_encode() expects parameter 2 to be long, string given in <b>/home/***/public_html/demo/maps/list.php</b> on line <b>38</b><br />
    Arkadaşım JSON_PRETTY_PRINT olmadan kullan. Php sürümün eskidir, ondan hata veriyordur. 5.4.0 lazım o seçenek için.
  • 12-11-2013, 21:52:03
    #5
    RHX adlı üyeden alıntı: mesajı görüntüle
    Öncelikle ilgin için teşekkür ederim ama aşağıdaki kodu
    <?php 
         
        header("Content-Type: text/plain; charset=UTF-8"); 
         
        $results = (object) array( 
            "status" => true, 
            "category" => (object) array( 
                "ID" => 1, 
                "title" => "title", 
                "description" => "description" 
            ), 
            "posts" => array( 
                (object) array( 
                    "ID" => 1, 
                    "type" => "post", 
                    "title" => "title", 
                    "content" => "content", 
                    "excerpt" => "excerpt", 
                    "published_at" => (time() - (72 * 60 * 60)), 
                    "comments" => array() 
                ), 
                (object) array( 
                    "ID" => 2, 
                    "type" => "post", 
                    "title" => "title", 
                    "content" => "content", 
                    "excerpt" => "excerpt", 
                    "published_at" => (time() - (24 * 60 * 60)), 
                    "comments" => array() 
                ) 
            ) 
        ); 
         
        print_r( 
            json_encode( 
                $results, 
                JSON_PRETTY_PRINT 
            ) 
        );
    1.php eklediğimde ve tarayıcıdan 1.php çalıştırdığımda şu hatayı almaktayım

    Warning: json_encode() expects parameter 2 to be long, string given in /home/***/public_html/demo/maps/list.php on line 38
    o parametreyi kaldırıp denesenizde olur. ben onu maksat görsellik olsun diye ekledim.
  • 12-11-2013, 21:55:10
    #6
    acayip adlı üyeden alıntı: mesajı görüntüle
    Arkadaşım JSON_PRETTY_PRINT olmadan kullan. Php sürümün eskidir, ondan hata veriyordur. 5.4.0 lazım o seçenek için.

    Evet dediğiniz gibi yaptım çalıştı sizde sağ olun. Tek bir nokta kaldı şimdi aşağıdaki kodun while ile tekrar etmesi gerekiyor ama tekrar edecek alana while yazdığımda doğal olarak hata veriyor.

    Tekrarlamaya çalıştığım alan:

    (object) array(  
                    "ID" => 1,  
                    "type" => "post",  
                    "title" => "title",  
                    "content" => "content",  
                    "excerpt" => "excerpt",  
                    "published_at" => (time() - (72 * 60 * 60)),  
                    "comments" => array()  
                ),
  • 12-11-2013, 22:01:59
    #7
    @RHX;

    <?php
    	
    	header("Content-Type: text/plain; charset=UTF-8");
    	
    	$results = (object) array(
    		"status" => true,
    		"category" => (object) array(
    			"ID" => 1,
    			"title" => "title",
    			"description" => "description"
    		),
    		"posts" => array()
    	);
    	
    	for($i = 1; $i <= 10; ++$i)
    		$results->posts[] = (object) array(
    			"ID" => $i,
    			"type" => "post_" . $i,
    			"title" => "title_" . $i,
    			"content" => "content_" . $i,
    			"excerpt" => "excerpt_" . $i,
    			"published_at" => (time() - ($i * 60 * 60)),
    			"comments" => array()
    		);
    	
    	### DÖNGÜYE ALMAK İÇİN ###
    	
    	foreach($results->posts as $post)
    		print_r($post);
  • 12-11-2013, 22:05:25
    #8
    saintx adlı üyeden alıntı: mesajı görüntüle
    @RHX;

    <?php
    	
    	header("Content-Type: text/plain; charset=UTF-8");
    	
    	$results = (object) array(
    		"status" => true,
    		"category" => (object) array(
    			"ID" => 1,
    			"title" => "title",
    			"description" => "description"
    		),
    		"posts" => array()
    	);
    	
    	for($i = 1; $i <= 10; ++$i)
    		$results->posts[] = (object) array(
    			"ID" => $i,
    			"type" => "post_" . $i,
    			"title" => "title_" . $i,
    			"content" => "content_" . $i,
    			"excerpt" => "excerpt_" . $i,
    			"published_at" => (time() - ($i * 60 * 60)),
    			"comments" => array()
    		);
    	
    	### DÖNGÜYE ALMAK İÇİN ###
    	
    	foreach($results->posts as $post)
    		print_r($post);

    Hocam bu şekilde yaptığımda çıktı istediğim formatta olmuyor. İstediğim şablon :

    {
        "status": true,
        "category": {
            "ID": 1,
            "title": "title",
            "description": "description"
        },
        "posts": [
            {
                "ID": 1,
                "type": "post",
                "title": "title",
                "content": "content",
                "excerpt": "excerpt",
                "published_at": 1384024870,
                "comments": [
    
                ]
            },
            {
                "ID": 2,
                "type": "post",
                "title": "title",
                "content": "content",
                "excerpt": "excerpt",
                "published_at": 1384197670,
                "comments": [
    
                ]
            }
        ]
    }
    Bu şekilde çıktı alabilir miyiz ? ama
    {
                "ID": 2,
                "type": "post",
                "title": "title",
                "content": "content",
                "excerpt": "excerpt",
                "published_at": 1384197670,
                "comments": [
    
                ]
            }
    içinde takrar edecek şekilde ?
  • 12-11-2013, 22:07:40
    #9
    RHX adlı üyeden alıntı: mesajı görüntüle
    Hocam bu şekilde yaptığımda çıktı istediğim formatta olmuyor. İstediğim şablon :

    {
        "status": true,
        "category": {
            "ID": 1,
            "title": "title",
            "description": "description"
        },
        "posts": [
            {
                "ID": 1,
                "type": "post",
                "title": "title",
                "content": "content",
                "excerpt": "excerpt",
                "published_at": 1384024870,
                "comments": [
    
                ]
            },
            {
                "ID": 2,
                "type": "post",
                "title": "title",
                "content": "content",
                "excerpt": "excerpt",
                "published_at": 1384197670,
                "comments": [
    
                ]
            }
        ]
    }
    Bu şekilde çıktı alabilir miyiz ? ama
    {
                "ID": 2,
                "type": "post",
                "title": "title",
                "content": "content",
                "excerpt": "excerpt",
                "published_at": 1384197670,
                "comments": [
    
                ]
            }
    içinde takrar edecek şekilde ?
    JSON sytanx standartlarına göre istemekte olduğunuz yapı uygun değil, kendi veri parser'inizi yazabilirseniz öyle bi' yapıyı kullanabilirsiniz.