我正在解析 json,但出了点问题。我在 LogCat 中收到此错误:
04-04 12:51:24.283: E/JSON Parser(9077): Error parsing data org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject
这是我正在解析 json 的函数:
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
JSONObject theJSON;
try {
// Getting Array of albums
theJSON = json.getJSONObject("json");
albums = theJSON.getJSONArray(TAG_ALBUMS);
// looping through All albums
for (int i = 0; i < albums.length(); i++) {
JSONObject c = albums.getJSONObject(i);
String album_id = c.getString(TAG_ID);
String album_name = c.getString(TAG_NAME);
String album_singer = c.getString(TAG_SINGER);
String album_mix = c.getString(TAG_MIX);
String album_genre = c.getString(TAG_GENRE);
String album_thumb = "http://nelu.burduja.com/musica/cover_image/"
+ c.getString(TAG_THUMB);
albumsArray.add(new Albums(album_id, album_name,
album_singer, album_genre, album_mix,
album_thumb, songsArray));
sngs = c.getJSONArray(TAG_SONGS);
// getting songs for the album
songsArray=new ArrayList<Songs>();
for (int j = 0; j < sngs.length(); j++) {
JSONObject songs = sngs.getJSONObject(j);
String song_artist = songs.getString(TAG_SONG_ARTIST);
String song_mp3 = songs.getString(TAG_SONG_MP3);
String song_mp4 = songs.getString(TAG_SONG_MP4);
String song_thumb = songs.getString(TAG_SONG_THUMB);
String song_title = songs.getString(TAG_SONG_TITLE);
songsArray.add(new Songs(song_title, song_artist,
song_mp3, song_mp4, song_thumb));
}
albumsArray.get(i).setSongs(songsArray);
}
} catch (JSONException e) {
e.printStackTrace();
}
这是我的 json 输出:
{
"json":{
"albums":[
{
"id":"1",
"name":"Summer hits",
"singer":"",
"genre":"Trance",
"mix":"yes",
"thumb":"cov_515aa81ff03a3.jpeg",
"songs":[
{
"song":"Song 1",
"artist":"Singer 1",
"mp3":"",
"mp4":"http://youtu.be/Jdra3HU6w0I",
"thumb":"snwith_515bf52a1d8e2.jpeg"
},
{
"song":"Song 3 ",
"artist":"Singer 3",
"mp3":"",
"mp4":"",
"thumb":"snwith_515bf55476e2c.jpeg"
},
{
"song":"Song 4",
"artist":"Singer 4",
"mp3":"",
"mp4":"",
"thumb":"snwith_515c14974562f.jpeg"
},
{
"song":"Super hit",
"artist":"Gherghe Topa",
"mp3":"with_515c34d1bbde7.mp3",
"mp4":"",
"thumb":"snwith_515c34d1aeee1.jpeg"
}
]
},
{
"id":"2",
"name":"Rock In",
"singer":"Limp Bizkit",
"genre":"Pop",
"mix":"no",
"thumb":"cov_515ab5aff24fe.jpeg",
"songs":[
{
"song":"In the end",
"artist":"Linkin park",
"mp3":"",
"mp4":"",
"thumb":""
},
{
"song":"December",
"artist":"Linkin park",
"mp3":"",
"mp4":"",
"thumb":""
},
{
"song":"Rock in baby",
"artist":"Linkin park",
"mp3":"",
"mp4":"",
"thumb":""
},
{
"song":"Bang head",
"artist":"Linkin park",
"mp3":"",
"mp4":"",
"thumb":""
}
]
},
{
"id":"3",
"name":"Hybrid Theory",
"singer":"Linkin park",
"genre":"Pop",
"mix":"no",
"thumb":"cov_515ab6010a98a.jpeg",
"songs":[
]
}
]
}
}
这是我完整的 logCat 错误日志:
04-04 12:51:24.283: E/JSON Parser(9077): Error parsing data org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject
04-04 12:51:24.283: W/dalvikvm(9077): threadid=11: thread exiting with uncaught exception (group=0x40a7e1f8)
04-04 12:51:24.293: E/AndroidRuntime(9077): FATAL EXCEPTION: AsyncTask #1
04-04 12:51:24.293: E/AndroidRuntime(9077): java.lang.RuntimeException: An error occured while executing doInBackground()
04-04 12:51:24.293: E/AndroidRuntime(9077): at android.os.AsyncTask$3.done(AsyncTask.java:278)
04-04 12:51:24.293: E/AndroidRuntime(9077): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-04 12:51:24.293: E/AndroidRuntime(9077): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-04 12:51:24.293: E/AndroidRuntime(9077): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-04 12:51:24.293: E/AndroidRuntime(9077): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-04 12:51:24.293: E/AndroidRuntime(9077): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
04-04 12:51:24.293: E/AndroidRuntime(9077): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-04 12:51:24.293: E/AndroidRuntime(9077): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-04 12:51:24.293: E/AndroidRuntime(9077): at java.lang.Thread.run(Thread.java:856)
04-04 12:51:24.293: E/AndroidRuntime(9077): Caused by: java.lang.NullPointerException
04-04 12:51:24.293: E/AndroidRuntime(9077): at com.myapp.activities.LatestAlbums$5.doInBackground(LatestAlbums.java:196)
04-04 12:51:24.293: E/AndroidRuntime(9077): at com.myapp.activities.LatestAlbums$5.doInBackground(LatestAlbums.java:1)
04-04 12:51:24.293: E/AndroidRuntime(9077): at android.os.AsyncTask$2.call(AsyncTask.java:264)
04-04 12:51:24.293: E/AndroidRuntime(9077): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-04 12:51:24.293: E/AndroidRuntime(9077): ... 5 more
我注意到如果我手动清除新行('\n)中的 json 输出以及对象和数组之间的空格,它可以正常工作。有人知道我该如何解决这个问题吗?