2012-06-27 2 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 객체를 사용하여 자바 스크립트 코드에서 가져 와서 파싱 할 수 있습니다.

$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

자바 스크립트 아마 당신은 배열로 모든 행을 수집하고 모든 행이 때 다음을 인코딩해야 {"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