可能重复:
JSON解析问题
我正在解析一个JSON文件(这是有效的).它适用于Android 4.0 - 4.0.4但不适用于较旧的Android版本.这是我的清单的一部分:
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="14" />
Run Code Online (Sandbox Code Playgroud)
这是我的解析代码:
public JSONObject getJSONFromUrl(String url) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line …Run Code Online (Sandbox Code Playgroud) 我想在我的Android应用程序中解析Json解析链接是https://www.buzzador.com/apps/present_software/webservice/index.php?op=ProductQ&campaign_id=607&userid=10776
当我把它放入Json对象时它会给我带来错误错误是:08-31 14:40:52.281:WARN/System.err(416):org.json.JSONException:java.lang.String类型的值无法转换到JSONObject
public static String getmyproductquestiondetails(String userid,
String campaignid) {// https://www.buzzador.com/apps/present_software/webservice/index.php?op=EducationResult&userid=1&questionid=1,2,3&answergivenbyuser=1,1,0
String data = null;
try {
URL url = new URL(
"http://dignizant.com/buzz/webservice/index.php?op=getProductQuestion&userid="
+ userid + "&campaign_id=" + campaignid);
if (url.getProtocol().toLowerCase().equals("https")) {
trustAllHosts();
HttpsURLConnection https = (HttpsURLConnection) url
.openConnection();
https.setHostnameVerifier(DO_NOT_VERIFY);
http = https;
} else {
http = (HttpURLConnection) url.openConnection();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Utils utils = new …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用从Web服务接收的以下字符串初始化JSONObject:
"{
"campaignid": "8",
"campaignname": "Pilotarienak 2011",
"campaignlink": "http:\\/\\/www.xxx.com\\/fr\\/cote-basque\\/agenda\\/2011-05-20\\/FMAAQU064FS016DV-pilotarienak-d-anglet?fromapp",
"splash": "http:\\/\\/www.xxx.com\\/ads\\/customers\\/pilotarienak\\/320x480.jpg",
"banner": "http:\\/\\/www.xxx.com\\/ads\\/customers\\/pilotarienak\\/320x160.jpg"
}"
Run Code Online (Sandbox Code Playgroud)
它似乎是有效的json(它在jsonlint.com中验证),但是在初始化JSONObject时,我得到:
org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
谢谢