2014-02-06 84 views
-2

是否可以將JSON字符串數組轉換爲java數組?將JSON字符串導入到java

對於類似這樣的http://api.soundrop.fm/spaces/XJTt3mXTOZpvgmOc

我需要獲取當前播放和藝術家值例子的東西。

+0

你寫一個應該讀一個Java程序來自類似於您的帖子中鏈接的文本文件的字符串?請詳細說明。 – SebastianH

+0

我認爲,因爲看到它是由一個網頁輸出它似乎很輕微,顯然我錯了。沒有文本文件,我有一個腳本來獲取鏈接的url並將其作爲字符串返回。 – Nonnisi

回答

1

我建議使用json-lib到JSON序列化爲Java對象。

然後,您可以從該對象獲取JSONArray,並將其綁定到其自己的數組中。

例子:

JSONObject json = new JSONObject(jsonString); 
JSONArray owners = (JSONArray) json.get("owners"); 

Iterator<String> iterator = owners.iterator(); 
while (iterator.hasNext()) { 
    System.out.println(iterator.next()); 
} 

輸出:

Wr3pWSGt54GlRiEL 
XJU7ZqZ3MYrQncIw 

格式化JSON:

{ 
    "_id":"XJTt3mXTOZpvgmOc", 
    "cover_picture":"http://images.soundrop.fm/spaces/XJTt3mXTOZpvgmOg/5I3f6HGSSob6c1TTzK8vHg", 
    "creator":"Wr3pWSGt54GlRiEL", 
    "description":"Radio for Okicraft minecraft server!\nwww.okicraft.com", 
    "display_name":"Molly's Bang'n Tunes FM AKA Okicraft Radio :D", 
    "display_picture":"http://images.soundrop.fm/spaces/XJTt3mXTOZpvgmOg/AiTQDQBYGg1wesnhFVLao", 
    "owners":[ 
     "Wr3pWSGt54GlRiEL", 
     "XJU7ZqZ3MYrQncIw" 
    ], 
    "parameters":{ 
     "soundrop:chat":{ 
     "version":2 
     }, 
     "soundrop:membership":{ 
     "version":2 
     }, 
     "soundrop:playlist":{ 
     "version":2, 
     "mode":"open" 
     } 
    }, 
    "presence":{ 
     "soundrop:chat":{ 

     }, 
     "soundrop:membership":{ 
     "members_live_count":0, 
     "members_total_count":189, 
     "members_max_count":8, 
     "members_max_date":"2014-02-01T19:33:29Z", 
     "members_summary":[ 

     ] 
     }, 
     "soundrop:playlist":{ 
     "current":{ 
      "_id":"WPg2zdgdMsBxsge7", 
      "title":"Son Of A Preacher Man", 
      "artists":[ 
       { 
        "_id":"WgCuwalktE9Wl4bS", 
        "display_name":"Dusty Springfield" 
       } 
      ], 
      "sources":[ 
       { 
        "album":"At Her Very Best", 
        "length":144241, 
        "uris":[ 
        "isrc:GBF086800754", 
        "spotify:track:02hv6aR5wa2Vhw4Xo8DaN2", 
        "spotify:track:3zRliQqUlisnnl3BguDkZM" 
        ] 
       }, 
       { 
        "album":"Dusty In Memphis", 
        "length":149365, 
        "uris":[ 
        "deezer:track:1104538", 
        "isrc:GBF086800754", 
        "isrc:USAT20702465", 
        "spotify:track:1IYeYU3r8CkP9ECrhdX9en", 
        "spotify:track:1pWd0ar7Y77ex8fWfaAyiq", 
        "spotify:track:26EuE0OC3dbqcpIyIvS13O", 
        "spotify:track:28jBolSSfkXDYYOZWTcHDE", 
        "spotify:track:3zRliQqUlisnnl3BguDkZM", 
        "spotify:track:4UCIi4gRVUG7x4nFGQiKmo", 
        "spotify:track:5TbLONSGZsR8cbhNEodCH6" 
        ] 
       }, 
       { 
        "album":"The Silver Collection", 
        "length":147823, 
        "uris":[ 
        "isrc:GBF086800754", 
        "spotify:track:0kHzYqfLwnfObM1v4E6LGI", 
        "spotify:track:0scrtPmtlIVwwk9s4LXJ8n", 
        "spotify:track:1pWd0ar7Y77ex8fWfaAyiq", 
        "spotify:track:3zRliQqUlisnnl3BguDkZM", 
        "spotify:track:4UCIi4gRVUG7x4nFGQiKmo" 
        ] 
       }, 
       { 
        "album":"Dusty In Memphis [Deluxe Edition]", 
        "length":149365, 
        "uris":[ 
        "isrc:USAT20702465", 
        "spotify:track:7odHgoLFi3GQ90E9PeraI3" 
        ] 
       }, 
       { 
        "album":"PULP FICTION Collector's Edition", 
        "length":146620, 
        "uris":[ 
        "isrc:GBF086800754", 
        "spotify:track:1pWd0ar7Y77ex8fWfaAyiq", 
        "spotify:track:1rojMZ3XzgmAZhc5PpVDec", 
        "spotify:track:3iDaK0Vsw1jRfuGjGBVU25", 
        "spotify:track:3zRliQqUlisnnl3BguDkZM", 
        "spotify:track:43MOKEiRYA05XEGnjnkMgS", 
        "spotify:track:55iXg89f5nRwBeFI39FX1p" 
        ] 
       }, 
       { 
        "album":"Drivetime", 
        "length":144895, 
        "uris":[ 
        "isrc:GBF086800754", 
        "spotify:track:1pWd0ar7Y77ex8fWfaAyiq", 
        "spotify:track:1rojMZ3XzgmAZhc5PpVDec", 
        "spotify:track:3zRliQqUlisnnl3BguDkZM", 
        "spotify:track:4b3YH93d0kEJ5lVvFtkiLP" 
        ] 
       }, 
       { 
        "album":"110% Oldies", 
        "length":145391, 
        "uris":[ 
        "isrc:GBF086800754", 
        "spotify:track:0kDT8dYWwdvuwfApPSU3TP", 
        "spotify:track:1pWd0ar7Y77ex8fWfaAyiq", 
        "spotify:track:3EqAYYYyhCYWebfA06CRBg", 
        "spotify:track:3zRliQqUlisnnl3BguDkZM" 
        ] 
       }, 
       { 
        "album":"Dusty Springfield Hit Pac - 5 Series", 
        "length":144215, 
        "uris":[ 
        "isrc:GBF086800754", 
        "spotify:track:1pWd0ar7Y77ex8fWfaAyiq", 
        "spotify:track:24IavnnaGG2AGYMdAwmVdn", 
        "spotify:track:3zRliQqUlisnnl3BguDkZM" 
        ] 
       }, 
       { 
        "album":"Movies", 
        "length":143953, 
        "uris":[ 
        "isrc:GBF086800754", 
        "spotify:track:1pWd0ar7Y77ex8fWfaAyiq", 
        "spotify:track:3zRliQqUlisnnl3BguDkZM", 
        "spotify:track:4gYl9m2oErJgwY2cu2TsuB", 
        "spotify:track:79awzAL3IzFfvo60KvAuIC" 
        ] 
       }, 
       { 
        "album":"Just Sing", 
        "length":146000, 
        "uris":[ 
        "isrc:GBF086800754", 
        "spotify:track:1pWd0ar7Y77ex8fWfaAyiq", 
        "spotify:track:3zRliQqUlisnnl3BguDkZM", 
        "spotify:track:4gYl9m2oErJgwY2cu2TsuB", 
        "spotify:track:65QTLn5NbBvLkR1U9aaHLA" 
        ] 
       }, 
       { 
        "album":"100 Essential Hits - Woman", 
        "length":144000, 
        "uris":[ 
        "isrc:GBF086800754", 
        "spotify:track:2QLTd3D6Cb79rbPMMxVkRo", 
        "spotify:track:3zRliQqUlisnnl3BguDkZM", 
        "spotify:track:55iXg89f5nRwBeFI39FX1p", 
        "spotify:track:7B4UpODwurpp47tbnpsRSb" 
        ] 
       }, 
       { 
        "album":"True Drivetime (3 CD Set)", 
        "length":145000, 
        "uris":[ 
        "deezer:track:910223", 
        "isrc:GBF086800754" 
        ] 
       }, 
       { 
        "album":"100 Soul", 
        "length":146000, 
        "uris":[ 
        "isrc:GBF086800754", 
        "spotify:track:09crwvurlODl7CYY7Tv1Ku", 
        "spotify:track:25jzJ7xqVTMsG4DAR50W48" 
        ] 
       }, 
       { 
        "album":"Just Dusty", 
        "length":145000, 
        "uris":[ 
        "isrc:GBF086800754", 
        "spotify:track:0knTTHUdnVn5CSblhcw7bS", 
        "spotify:track:0scrtPmtlIVwwk9s4LXJ8n", 
        "spotify:track:3zRliQqUlisnnl3BguDkZM", 
        "spotify:track:6gmeXarVRe4ZBBKjXepRAC", 
        "spotify:track:7odHgoLFi3GQ90E9PeraI3" 
        ] 
       } 
      ] 
     } 
     } 
    }, 
    "status":"normal", 
    "tags":[ 
     "minecraft" 
    ] 
} 
0

這是JSON格式,或至少它看起來像它,也有JSON的Java解析器所以請嘗試使用它,它就會產生地圖,而不是一個數組