doujiang5211 2015-05-29 19:31
浏览 43
已采纳

Php To Json如何转换?

We can convert php code how to json format? I may have not accurately PHP coding I am beginner to learn about it because I'm new . I'll integrated into Android application. I also draw pictures about how the information ?

for example, I want to do something like this: http://mikepenz.com/android/unsplash/pictures

<?php
// don't forget to change 'username' to your actual tumblr name
$request = 'http://walltumbler.tumblr.com/api/read/json';
$ci = curl_init($request);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
$input = curl_exec($ci);
// Tumblr JSON doesn't come in standard form, some str replace needed
$input = str_replace('var tumblr_api_read = ','',$input);
$input = str_replace(';','',$input);
// parameter 'true' is necessary for output as PHP array
$value = json_decode($input, true);
$content =  $value['posts'];
// the number of items you want to display
$item = 98988;
// Tumblr provides various photo size, this case will choose the 75x75 square one
$type = 'photo-url-1280';


?>
{
    "limit": null,
    "offset": 0,
    "count": 2442,
    "total": 2442,
    "data": [

<?php

for ($i=0;$i<=$item;$i++) {
    if ($content[$i]['type'] == 'photo') {
        echo '
          {
            "id": '.$i.';
            "author": "Paul Jarvis",
            "image_src": "' . $content[$i][$type] . '",
            "color": "#7F7873",
            "date": "2015-01-21 19:20:00",
            "modified_date": "2014-09-01 22:36:53",
            "width": 2500,
            "height": 1667,
            "ratio": 1.4997000694275,
            "featured": 1,
            "temp_id": 1
        }';
      $string = rtrim($item, ', ');
    }
}
?>
]}
  • 写回答

1条回答 默认 最新

  • douwei4370 2015-05-29 19:35
    关注

    Try using the json_encode() function

    <?php
    
    $request = 'http://walltumbler.tumblr.com/api/read/json';
    $ci = curl_init($request);
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
    $input = curl_exec($ci);
    $input = str_replace('var tumblr_api_read = ','',$input);
    $input = str_replace(';','',$input);
    $value = json_decode($input, true);
    $content =  $value['posts'];
    $item = 98988;
    $type = 'photo-url-1280';
    
    $photos_array = array();
    
    for ($i=0;$i<=$item;$i++) {
        if ($content[$i]['type'] == 'photo') {
            $photos_array[] = array(
                'id' => $i,
                'author' => 'Paul Jarvis',
                // Continue with all your values...
            );
        }
    }
    
    $json_data = array(
        'limit'  => null,
        'offset' => 0,
        'count'  => 2442,
        'total'  => 2442,
        'data'   => $photos_array
    );
    
    // Then use json_encode to get your json data...
    
    echo json_encode( $json_data );
    

    Hope it helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题