我正在製作一個主要用JavaScript和Flash編寫的LinkedIn應用程序,但所有數據都來自Java代理。我需要使用JSON數據,不幸的是,LinkedIn只支持XML。最好的解決方案是在將XML發送回客戶端之前將XML轉換爲JSON,但我承認我的Java技能並不強大。我有看起來應該工作的代碼,但我得到一個JSONObject異常。將LinkedIn XML數據轉換爲Java中的JSON
我使用org.json包來操作XML:http://json.org/java/
這是一種嘗試XML轉換成JSON的Java代碼。這不是很漂亮,但我只是想取得一些進展與轉換數據:
public static String readResponse(HttpResponse response) {
System.out.println("Reading response...");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String readLine;
String innhold = "";
while (((readLine = br.readLine()) != null)) {
innhold += readLine;
}
try {
JSONObject myJ = new JSONObject();
String ret = myJ.getJSONObject(innhold).toString();
System.out.println(ret);
return ret;
} catch (Exception e) {
System.out.println(e);
}
return innhold;
} catch (IOException e) {
System.out.println(e);
return null;
}
}
下面是數據非常相似,我想轉換:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
<first-name>First</first-name>
<last-name>Last</last-name>
<headline>My Profile</headline>
<site-standard-profile-request>
<url>http://www.linkedin.com/profile</url>
</site-standard-profile-request>
</person>
這裏是異常我越來越:
org.json.JSONException: JSONObject["<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><person> <first-name>First<\/first-name> <last-name>Last<\/last-name> <headline>My Profile<\/headline> <site-standard-profile-request> <url>http://www.linkedin.com/profile<\/url> <\/site-standard-profile-request><\/person>"] not found.
任何幫助表示讚賞,謝謝!