我已尝试过所有内容,但我无法将此JSON对象转换为JSON数组,它会抛出JSON异常
这是Logcat:
11-13 11:19:53.964:W / System.err(8642):at org.json.JSON.typeMismatch(JSON.java:111) 11-13 11:19:53.964:W / System.err(8642):at org.json.JSONArray。(JSONArray.java:91) 11-13 11:19:53.964:W / System.err(8642):at org.json.JSONArray。(JSONArray.java:103)
这是JSON文件:
{ “GetGISResult”:[{ “活动”:真, “类别ID”:1, “CREATEDATE”:NULL, “CREATEUSER”:NULL, “Description_ar”: “التا”, “Description_en”: “هعلل”, “电子邮件”: “لتتالت”, “纬度”:30.0764857, “龙”:31.30658, “手机”: “تالتال”, “SectionID”:9, “SectionName_ar”: “1”, “SectionName_en”: “لاتا” “UpdateDate”:空, “UpdateUser两个”:空},{ “活动”:真正的 “类别ID”:1, “CREATEDATE”:空, “CREATEUSER”:空, “Description_ar”: “التا”, “Description_en”: “هعلل”, “电子邮件”: “لتتالت”, “纬度”:30.0892124, “龙”:31.2892342, “手机”: “تالتال”, “SectionID”:10, “SectionName_ar”: “2”, “SectionName_en”: “لاتا”, “UpdateDate”:空, “UpdateUser两个”:空},{ “活动”:真正的 “类别ID”:1, “CREATEDATE”:空, “CREATEUSER”:空, “Description_ar”: “التا” “Description_en”: “هعلل”, “电子邮件”: “لتتالت”, “纬度”:30.0433341, “龙”:31.3006067, “手机”: “تالتال”, “SectionID”:15, “SectionName_ar”: “7” “SectionName_en”: “لاتا”, “UpdateDate”:NULL, “UpdateUser两个”:空}]}
这是我的代码:
try {
HttpResponse response = httpClient.execute(httpGet);
int responsecode = response.getStatusLine().getStatusCode();
if (responsecode == 200) {
InputStream in = response.getEntity().getContent();
resultstring = convertinputStreamToString(in);
System.out.println("resultstring>>> "+resultstring);
try {
JSONArray stores = new JSONArray(resultstring);
for (int i = 0; i < stores.length(); ++i) {
JSONObject jo = (JSONObject) stores.get(i);
// TO-DO JSON here
result.add(convertToStores(jo));
Log.v("check on adding Lat to list", result.get(i)
.getLat()+"");
Log.v("check on adding Lon to list", result.get(i)
.getLong()+"");
Log.v("check on adding Description to list",
result.get(i).getDescription_ar());
Log.v("check on adding Email to list",
result.get(i).getEmail());
Log.v("check on adding Mobile to list", result
.get(i).getMobile());
}
} catch (JSONException c) {
Log.v("Exception >>>", c.getMessage().toString());
c.printStackTrace();
}
long total = 0;
byte data[] = new byte[1024];
while ((count = in.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
setProgress((int)((total*100)/1000));
}
}else if(responsecode != 200){
Toast.makeText(getApplicationContext(), "Your connection is break down please check your connection ..", Toast.LENGTH_LONG).show();
}
public static String convertinputStreamToString(InputStream ists)
throws IOException {
if (ists != null) {
StringBuilder sb = new StringBuilder();
String line;
try {
BufferedReader r1 = new BufferedReader(new InputStreamReader(
ists, "UTF-8"));
while ((line = r1.readLine()) != null) {
sb.append(line).append("\n");
}
} finally {
ists.close();
}
return sb.toString();
} else {
return "";
}
}
答案 0 :(得分:2)
首先你必须为结果创建jsonObject。
JSONObject jsonObj=new JSONObject(resultString);
JSONArray array=jsonObj.getJSONArray("GetGISResult");
答案 1 :(得分:1)
你的json解析代码是错误的。尝试以下代码:
JSONObject jsObject=new JSONObject(resultString);
JSONArray jsArry = jsObject.getJSONArray("GetGISResult");
for(int i=0; i<jsArry.length();i++){
JSONObject jsOrderDataObject = jsArry.getJSONObject(i);
jsOrderDataObject.getString("CategoryID");
.......... and so on
}