2012-06-27 206 views
0

下面是其中餘米嘗試轉換成JSON對象的數組:轉換陣列樹狀JSON結構

Array 
(
    [0] => 102 
    [id] => 102 
    [1] => 
    [attribution] => 
    [2] => image 
    [type] => image 
    [3] => 
    [latitude] => 
    [4] => 
    [longitude] => 
    [5] => 81 
    [comments] => 81 
    [6] => Nashville 
    [filter] => Nashville 
    [7] => 1340803981 
    [created_time] => 1340803981 
    [8] => http://instagr.am/p/MYQ4IqTVHm/ 
    [link] => http://instagr.am/p/MYQ4IqTVHm/ 
    [9] => 1847 
    [likes] => 1847 
    [10] => http://distilleryimage6.s3.amazonaws.com/9dcd44d2c05c11e1a39b1231381b7ba1_6.jpg 
    [low_resolution] => http://distilleryimage6.s3.amazonaws.com/9dcd44d2c05c11e1a39b1231381b7ba1_6.jpg 
    [11] => http://distilleryimage6.s3.amazonaws.com/9dcd44d2c05c11e1a39b1231381b7ba1_5.jpg 
    [thumbnail] => http://distilleryimage6.s3.amazonaws.com/9dcd44d2c05c11e1a39b1231381b7ba1_5.jpg 
    [12] => http://distilleryimage6.s3.amazonaws.com/9dcd44d2c05c11e1a39b1231381b7ba1_7.jpg 
    [standard_resolution] => http://distilleryimage6.s3.amazonaws.com/9dcd44d2c05c11e1a39b1231381b7ba1_7.jpg 
    [13] => 223002407889359334_26499947 
    [pid] => 223002407889359334_26499947 
    [14] => doutzenkroes1 
    [username] => doutzenkroes1 
    [15] => 
    [website] => 
    [16] => 
    [bio] => 
    [17] => http://images.instagram.com/profiles/profile_26499947_75sq_1335638089.jpg 
    [profile_picture] => http://images.instagram.com/profiles/profile_26499947_75sq_1335638089.jpg 
    [18] => Doutzen Kroes 
    [full_name] => Doutzen Kroes 
    [19] => 26499947 
    [uid] => 26499947 
) 
Array 
(
    [0] => 101 
    [id] => 101 
    [1] => 
    [attribution] => 
    [2] => image 
    [type] => image 
    [3] => 13.827936172 
    [latitude] => 13.827936172 
    [4] => 100.471916198 
    [longitude] => 100.471916198 
    [5] => 20 
    [comments] => 20 
    [6] => Brannan 
    [filter] => Brannan 
    [7] => 1340802429 
    [created_time] => 1340802429 
    [8] => http://instagr.am/p/MYN62aqdE8/ 
    [link] => http://instagr.am/p/MYN62aqdE8/ 
    [9] => 1497 
    [likes] => 1497 
    [10] => http://distilleryimage4.s3.amazonaws.com/00df3f7ac05911e1b2fe1231380205bf_6.jpg 
    [low_resolution] => http://distilleryimage4.s3.amazonaws.com/00df3f7ac05911e1b2fe1231380205bf_6.jpg 
    [11] => http://distilleryimage4.s3.amazonaws.com/00df3f7ac05911e1b2fe1231380205bf_5.jpg 
    [thumbnail] => http://distilleryimage4.s3.amazonaws.com/00df3f7ac05911e1b2fe1231380205bf_5.jpg 
    [12] => http://distilleryimage4.s3.amazonaws.com/00df3f7ac05911e1b2fe1231380205bf_7.jpg 
    [standard_resolution] => http://distilleryimage4.s3.amazonaws.com/00df3f7ac05911e1b2fe1231380205bf_7.jpg 
    [13] => 222989400318529852_1387675 
    [pid] => 222989400318529852_1387675 
    [14] => wan_soloist 
    [username] => wan_soloist 
    [15] => http://www.facebook.com/WanThanakrit 
    [website] => http://www.facebook.com/WanThanakrit 
    [16] => Soloist SnapShot & Last Lemonade~!! 
    [bio] => Soloist SnapShot & Last Lemonade~!! 
    [17] => http://images.instagram.com/profiles/profile_1387675_75sq_1334119071.jpg 
    [profile_picture] => http://images.instagram.com/profiles/profile_1387675_75sq_1334119071.jpg 
    [18] => Wan Soloist 
    [full_name] => Wan Soloist 
    [19] => 1387675 
    [uid] => 1387675 
) 
Array 
(
    [0] => 100 
    [id] => 100 
    [1] => 
    [attribution] => 
    [2] => image 
    [type] => image 
    [3] => 
    [latitude] => 
    [4] => 
    [longitude] => 
    [5] => 25 
    [comments] => 25 
    [6] => Normal 
    [filter] => Normal 
    [7] => 1340804961 
    [created_time] => 1340804961 
    [8] => http://instagr.am/p/MYSwEjCzRq/ 
    [link] => http://instagr.am/p/MYSwEjCzRq/ 
    [9] => 1544 
    [likes] => 1544 
    [10] => http://distilleryimage10.s3.amazonaws.com/e5ad1776c05e11e19dc71231380fe523_6.jpg 
    [low_resolution] => http://distilleryimage10.s3.amazonaws.com/e5ad1776c05e11e19dc71231380fe523_6.jpg 
    [11] => http://distilleryimage10.s3.amazonaws.com/e5ad1776c05e11e19dc71231380fe523_5.jpg 
    [thumbnail] => http://distilleryimage10.s3.amazonaws.com/e5ad1776c05e11e19dc71231380fe523_5.jpg 
    [12] => http://distilleryimage10.s3.amazonaws.com/e5ad1776c05e11e19dc71231380fe523_7.jpg 
    [standard_resolution] => http://distilleryimage10.s3.amazonaws.com/e5ad1776c05e11e19dc71231380fe523_7.jpg 
    [13] => 223010649809826922_10380716 
    [pid] => 223010649809826922_10380716 
    [14] => amarelloo 
    [username] => amarelloo 
    [15] => 
    [website] => 
    [16] => 
    [bio] => 
    [17] => http://images.instagram.com/profiles/profile_10380716_75sq_1338715577.jpg 
    [profile_picture] => http://images.instagram.com/profiles/profile_10380716_75sq_1338715577.jpg 
    [18] => Marcelo Peña 
    [full_name] => Marcelo Peña 
    [19] => 10380716 
    [uid] => 10380716 
) 
Array 
(
    [0] => 99 
    [id] => 99 
    [1] => 
    [attribution] => 
    [2] => image 
    [type] => image 
    [3] => 
    [latitude] => 
    [4] => 
    [longitude] => 
    [5] => 43 
    [comments] => 43 
    [6] => Sutro 
    [filter] => Sutro 
    [7] => 1340799635 
    [created_time] => 1340799635 
    [8] => http://instagr.am/p/MYIlthLZhQ/ 
    [link] => http://instagr.am/p/MYIlthLZhQ/ 
    [9] => 1412 
    [likes] => 1412 
    [10] => http://distilleryimage4.s3.amazonaws.com/7f18b29cc05211e1b00112313800c5e4_6.jpg 
    [low_resolution] => http://distilleryimage4.s3.amazonaws.com/7f18b29cc05211e1b00112313800c5e4_6.jpg 
    [11] => http://distilleryimage4.s3.amazonaws.com/7f18b29cc05211e1b00112313800c5e4_5.jpg 
    [thumbnail] => http://distilleryimage4.s3.amazonaws.com/7f18b29cc05211e1b00112313800c5e4_5.jpg 
    [12] => http://distilleryimage4.s3.amazonaws.com/7f18b29cc05211e1b00112313800c5e4_7.jpg 
    [standard_resolution] => http://distilleryimage4.s3.amazonaws.com/7f18b29cc05211e1b00112313800c5e4_7.jpg 
    [13] => 222965957422585936_11252917 
    [pid] => 222965957422585936_11252917 
    [14] => stevesototattoo 
    [username] => stevesototattoo 
    [15] => http://www.stevesototattoo.com 
    [website] => http://www.stevesototattoo.com 
    [16] => Tattoo Artist & Owner of Goodfellas Tattoo Art & Design Studio. World Traveling Tattoo Artist. West Coast Reppin 
    [bio] => Tattoo Artist & Owner of Goodfellas Tattoo Art & Design Studio. World Traveling Tattoo Artist. West Coast Reppin 
    [17] => http://images.instagram.com/profiles/profile_11252917_75sq_1339634703.jpg 
    [profile_picture] => http://images.instagram.com/profiles/profile_11252917_75sq_1339634703.jpg 
    [18] => Steve Soto Tattoo Art Co. 
    [full_name] => Steve Soto Tattoo Art Co. 
    [19] => 11252917 
    [uid] => 11252917 
) 
Array 
(
    [0] => 98 
    [id] => 98 
    [1] => 
    [attribution] => 
    [2] => image 
    [type] => image 
    [3] => 
    [latitude] => 
    [4] => 
    [longitude] => 
    [5] => 83 
    [comments] => 83 
    [6] => Valencia 
    [filter] => Valencia 
    [7] => 1340800009 
    [created_time] => 1340800009 
    [8] => http://instagr.am/p/MYJTYbhVND/ 
    [link] => http://instagr.am/p/MYJTYbhVND/ 
    [9] => 1824 
    [likes] => 1824 
    [10] => http://distilleryimage11.s3.amazonaws.com/5e24e56ec05311e1af7612313813f8e8_6.jpg 
    [low_resolution] => http://distilleryimage11.s3.amazonaws.com/5e24e56ec05311e1af7612313813f8e8_6.jpg 
    [11] => http://distilleryimage11.s3.amazonaws.com/5e24e56ec05311e1af7612313813f8e8_5.jpg 
    [thumbnail] => http://distilleryimage11.s3.amazonaws.com/5e24e56ec05311e1af7612313813f8e8_5.jpg 
    [12] => http://distilleryimage11.s3.amazonaws.com/5e24e56ec05311e1af7612313813f8e8_7.jpg 
    [standard_resolution] => http://distilleryimage11.s3.amazonaws.com/5e24e56ec05311e1af7612313813f8e8_7.jpg 
    [13] => 222969095875023683_3745339 
    [pid] => 222969095875023683_3745339 
    [14] => namidacross 
    [username] => namidacross 
    [15] => 
    [website] => 
    [16] => Art Director/Graphic Designer // Japan 

My another account 
B&W only â–· @namidacrossbw 

    [bio] => Art Director/Graphic Designer // Japan 

My another account 
B&W only â–· @namidacrossbw 

    [17] => http://images.instagram.com/profiles/profile_3745339_75sq_1315794772.jpg 
    [profile_picture] => http://images.instagram.com/profiles/profile_3745339_75sq_1315794772.jpg 
    [18] => NAMiDA+ 
    [full_name] => NAMiDA+ 
    [19] => 3745339 
    [uid] => 3745339 
) 

及這裏的我米使用,以獲取它,並將其轉換爲有效的代碼json對象,以便我的Javascript代碼可以獲取並解析它。

$query = mysql_query("SELECT * FROM `table_name` ORDER BY id DESC LIMIT 5") or die(mysql_error()); // fetching the latest 5 rows 
    while ($result = mysql_fetch_array($query)) { 
     //echo "<pre>"; print_r($result); echo "</pre>"; 
     print (json_encode($result)); 
    } 

但我得到的迴應json_encode似乎並不是一個有效的json對象! 我在做什麼錯?

下面是發出JSON對象

{ 
    "0": "102", 
    "id": "102", 
    "1": "", 
    "attribution": "", 
    "2": "image", 
    "type": "image", 
    "3": "", 
    "latitude": "", 
    "4": "", 
    "longitude": "", 
    "5": "81", 
    "comments": "81", 
    "6": "Nashville", 
    "filter": "Nashville", 
    "7": "1340803981", 
    "created_time": "1340803981", 
    "8": "http:\/\/instagr.am\/p\/MYQ4IqTVHm\/", 
    "link": "http:\/\/instagr.am\/p\/MYQ4IqTVHm\/", 
    "9": "1847", 
    "likes": "1847", 
    "10": "http:\/\/distilleryimage6.s3.amazonaws.com\/9dcd44d2c05c11e1a39b1231381b7ba1_6.jpg", 
    "low_resolution": "http:\/\/distilleryimage6.s3.amazonaws.com\/9dcd44d2c05c11e1a39b1231381b7ba1_6.jpg", 
    "11": "http:\/\/distilleryimage6.s3.amazonaws.com\/9dcd44d2c05c11e1a39b1231381b7ba1_5.jpg", 
    "thumbnail": "http:\/\/distilleryimage6.s3.amazonaws.com\/9dcd44d2c05c11e1a39b1231381b7ba1_5.jpg", 
    "12": "http:\/\/distilleryimage6.s3.amazonaws.com\/9dcd44d2c05c11e1a39b1231381b7ba1_7.jpg", 
    "standard_resolution": "http:\/\/distilleryimage6.s3.amazonaws.com\/9dcd44d2c05c11e1a39b1231381b7ba1_7.jpg", 
    "13": "223002407889359334_26499947", 
    "pid": "223002407889359334_26499947", 
    "14": "doutzenkroes1", 
    "username": "doutzenkroes1", 
    "15": "", 
    "website": "", 
    "16": "", 
    "bio": "", 
    "17": "http:\/\/images.instagram.com\/profiles\/profile_26499947_75sq_1335638089.jpg", 
    "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_26499947_75sq_1335638089.jpg", 
    "18": "Doutzen Kroes", 
    "full_name": "Doutzen Kroes", 
    "19": "26499947", 
    "uid": "26499947" 
} { 
    "0": "101", 
    "id": "101", 
    "1": "", 
    "attribution": "", 
    "2": "image", 
    "type": "image", 
    "3": "13.827936172", 
    "latitude": "13.827936172", 
    "4": "100.471916198", 
    "longitude": "100.471916198", 
    "5": "20", 
    "comments": "20", 
    "6": "Brannan", 
    "filter": "Brannan", 
    "7": "1340802429", 
    "created_time": "1340802429", 
    "8": "http:\/\/instagr.am\/p\/MYN62aqdE8\/", 
    "link": "http:\/\/instagr.am\/p\/MYN62aqdE8\/", 
    "9": "1497", 
    "likes": "1497", 
    "10": "http:\/\/distilleryimage4.s3.amazonaws.com\/00df3f7ac05911e1b2fe1231380205bf_6.jpg", 
    "low_resolution": "http:\/\/distilleryimage4.s3.amazonaws.com\/00df3f7ac05911e1b2fe1231380205bf_6.jpg", 
    "11": "http:\/\/distilleryimage4.s3.amazonaws.com\/00df3f7ac05911e1b2fe1231380205bf_5.jpg", 
    "thumbnail": "http:\/\/distilleryimage4.s3.amazonaws.com\/00df3f7ac05911e1b2fe1231380205bf_5.jpg", 
    "12": "http:\/\/distilleryimage4.s3.amazonaws.com\/00df3f7ac05911e1b2fe1231380205bf_7.jpg", 
    "standard_resolution": "http:\/\/distilleryimage4.s3.amazonaws.com\/00df3f7ac05911e1b2fe1231380205bf_7.jpg", 
    "13": "222989400318529852_1387675", 
    "pid": "222989400318529852_1387675", 
    "14": "wan_soloist", 
    "username": "wan_soloist", 
    "15": "http:\/\/www.facebook.com\/WanThanakrit", 
    "website": "http:\/\/www.facebook.com\/WanThanakrit", 
    "16": "Soloist SnapShot & Last Lemonade~!!", 
    "bio": "Soloist SnapShot & Last Lemonade~!!", 
    "17": "http:\/\/images.instagram.com\/profiles\/profile_1387675_75sq_1334119071.jpg", 
    "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_1387675_75sq_1334119071.jpg", 
    "18": "Wan Soloist", 
    "full_name": "Wan Soloist", 
    "19": "1387675", 
    "uid": "1387675" 
} { 
    "0": "100", 
    "id": "100", 
    "1": "", 
    "attribution": "", 
    "2": "image", 
    "type": "image", 
    "3": "", 
    "latitude": "", 
    "4": "", 
    "longitude": "", 
    "5": "25", 
    "comments": "25", 
    "6": "Normal", 
    "filter": "Normal", 
    "7": "1340804961", 
    "created_time": "1340804961", 
    "8": "http:\/\/instagr.am\/p\/MYSwEjCzRq\/", 
    "link": "http:\/\/instagr.am\/p\/MYSwEjCzRq\/", 
    "9": "1544", 
    "likes": "1544", 
    "10": "http:\/\/distilleryimage10.s3.amazonaws.com\/e5ad1776c05e11e19dc71231380fe523_6.jpg", 
    "low_resolution": "http:\/\/distilleryimage10.s3.amazonaws.com\/e5ad1776c05e11e19dc71231380fe523_6.jpg", 
    "11": "http:\/\/distilleryimage10.s3.amazonaws.com\/e5ad1776c05e11e19dc71231380fe523_5.jpg", 
    "thumbnail": "http:\/\/distilleryimage10.s3.amazonaws.com\/e5ad1776c05e11e19dc71231380fe523_5.jpg", 
    "12": "http:\/\/distilleryimage10.s3.amazonaws.com\/e5ad1776c05e11e19dc71231380fe523_7.jpg", 
    "standard_resolution": "http:\/\/distilleryimage10.s3.amazonaws.com\/e5ad1776c05e11e19dc71231380fe523_7.jpg", 
    "13": "223010649809826922_10380716", 
    "pid": "223010649809826922_10380716", 
    "14": "amarelloo", 
    "username": "amarelloo", 
    "15": "", 
    "website": "", 
    "16": "", 
    "bio": "", 
    "17": "http:\/\/images.instagram.com\/profiles\/profile_10380716_75sq_1338715577.jpg", 
    "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_10380716_75sq_1338715577.jpg", 
    "18": "Marcelo Pe\u00f1a", 
    "full_name": "Marcelo Pe\u00f1a", 
    "19": "10380716", 
    "uid": "10380716" 
} { 
    "0": "99", 
    "id": "99", 
    "1": "", 
    "attribution": "", 
    "2": "image", 
    "type": "image", 
    "3": "", 
    "latitude": "", 
    "4": "", 
    "longitude": "", 
    "5": "43", 
    "comments": "43", 
    "6": "Sutro", 
    "filter": "Sutro", 
    "7": "1340799635", 
    "created_time": "1340799635", 
    "8": "http:\/\/instagr.am\/p\/MYIlthLZhQ\/", 
    "link": "http:\/\/instagr.am\/p\/MYIlthLZhQ\/", 
    "9": "1412", 
    "likes": "1412", 
    "10": "http:\/\/distilleryimage4.s3.amazonaws.com\/7f18b29cc05211e1b00112313800c5e4_6.jpg", 
    "low_resolution": "http:\/\/distilleryimage4.s3.amazonaws.com\/7f18b29cc05211e1b00112313800c5e4_6.jpg", 
    "11": "http:\/\/distilleryimage4.s3.amazonaws.com\/7f18b29cc05211e1b00112313800c5e4_5.jpg", 
    "thumbnail": "http:\/\/distilleryimage4.s3.amazonaws.com\/7f18b29cc05211e1b00112313800c5e4_5.jpg", 
    "12": "http:\/\/distilleryimage4.s3.amazonaws.com\/7f18b29cc05211e1b00112313800c5e4_7.jpg", 
    "standard_resolution": "http:\/\/distilleryimage4.s3.amazonaws.com\/7f18b29cc05211e1b00112313800c5e4_7.jpg", 
    "13": "222965957422585936_11252917", 
    "pid": "222965957422585936_11252917", 
    "14": "stevesototattoo", 
    "username": "stevesototattoo", 
    "15": "http:\/\/www.stevesototattoo.com", 
    "website": "http:\/\/www.stevesototattoo.com", 
    "16": "Tattoo Artist & Owner of Goodfellas Tattoo Art & Design Studio. World Traveling Tattoo Artist. West Coast Reppin", 
    "bio": "Tattoo Artist & Owner of Goodfellas Tattoo Art & Design Studio. World Traveling Tattoo Artist. West Coast Reppin", 
    "17": "http:\/\/images.instagram.com\/profiles\/profile_11252917_75sq_1339634703.jpg", 
    "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_11252917_75sq_1339634703.jpg", 
    "18": "Steve Soto Tattoo Art Co.", 
    "full_name": "Steve Soto Tattoo Art Co.", 
    "19": "11252917", 
    "uid": "11252917" 
} { 
    "0": "98", 
    "id": "98", 
    "1": "", 
    "attribution": "", 
    "2": "image", 
    "type": "image", 
    "3": "", 
    "latitude": "", 
    "4": "", 
    "longitude": "", 
    "5": "83", 
    "comments": "83", 
    "6": "Valencia", 
    "filter": "Valencia", 
    "7": "1340800009", 
    "created_time": "1340800009", 
    "8": "http:\/\/instagr.am\/p\/MYJTYbhVND\/", 
    "link": "http:\/\/instagr.am\/p\/MYJTYbhVND\/", 
    "9": "1824", 
    "likes": "1824", 
    "10": "http:\/\/distilleryimage11.s3.amazonaws.com\/5e24e56ec05311e1af7612313813f8e8_6.jpg", 
    "low_resolution": "http:\/\/distilleryimage11.s3.amazonaws.com\/5e24e56ec05311e1af7612313813f8e8_6.jpg", 
    "11": "http:\/\/distilleryimage11.s3.amazonaws.com\/5e24e56ec05311e1af7612313813f8e8_5.jpg", 
    "thumbnail": "http:\/\/distilleryimage11.s3.amazonaws.com\/5e24e56ec05311e1af7612313813f8e8_5.jpg", 
    "12": "http:\/\/distilleryimage11.s3.amazonaws.com\/5e24e56ec05311e1af7612313813f8e8_7.jpg", 
    "standard_resolution": "http:\/\/distilleryimage11.s3.amazonaws.com\/5e24e56ec05311e1af7612313813f8e8_7.jpg", 
    "13": "222969095875023683_3745339", 
    "pid": "222969095875023683_3745339", 
    "14": "namidacross", 
    "username": "namidacross", 
    "15": "", 
    "website": "", 
    "16": "Art Director \/ Graphic Designer \/\/ Japan\r\n\r\nMy another account \r\nB&W only \u25b7 @namidacrossbw\r\n", 
    "bio": "Art Director \/ Graphic Designer \/\/ Japan\r\n\r\nMy another account \r\nB&W only \u25b7 @namidacrossbw\r\n", 
    "17": "http:\/\/images.instagram.com\/profiles\/profile_3745339_75sq_1315794772.jpg", 
    "profile_picture": "http:\/\/images.instagram.com\/profiles\/profile_3745339_75sq_1315794772.jpg", 
    "18": "NAMiDA+", 
    "full_name": "NAMiDA+", 
    "19": "3745339", 
    "uid": "3745339" 
} 
+2

爲什麼'json_encode'發出了什麼?其實你正在打印獨立編碼的json對象,它不會自動形成一個大的行json。 – biziclop

回答

4

您的JavaScript可能收到類似的{"id":1,...}{"id":2}{...}{...}代替[{"id":1,...},{"id":2},{...},{...},....]

你應該收集所有的行到一個數組,而當你把所有的行,然後編碼行數組:

$query = mysql_query("SELECT * FROM `table_name` ORDER BY id DESC LIMIT 5"); 
$rows = array(); 

    while ($result = mysql_fetch_assoc($query)) { 
     $rows[] = $result; 
    } 
    echo json_encode($rows); 
+0

哇!那很簡單 !!! –

+0

有什麼辦法讓我可以用漂亮的格式打印json嗎? –

+0

從PHP 5.4開始,'json_encode'可以解釋可能會優化輸出的選項'JSON_PRETTY_PRINT',但我不相信PHP的製作者的高品味:) – biziclop