0

这是我试图转换为 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"
}
4

1 回答 1

3

您的 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 );
于 2012-06-27T20:29:28.580 回答