我正在尝试解析我的Android应用程序中的json(进一步我将用内容填充listview),所以我创建了这个类:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ReadJSONFeedTask().execute("http://10.14.182.253/viztonjob/update.php?updatedAt=01011970");
}
public String readJSONFeed(String URL) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
} else {
Log.e("JSON", "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
private class ReadJSONFeedTask extends AsyncTask<String, Void, String> {
private final ProgressDialog pd = new ProgressDialog(MainActivity.this);
protected String doInBackground(String... urls) {
return readJSONFeed(urls[0]);
}
protected void onPreExecute() {
this.pd.setMessage("Chargement de la liste des métiers ...");
this.pd.show();
}
protected void onPostExecute(String result) {
pd.dismiss();
try {
JSONArray jsonArray = new JSONArray(result);
Log.i("JSON",
"Number of surveys in feed: " + jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Toast.makeText(
getBaseContext(),
jsonObject.getString("metiers") + " - "
+ jsonObject.getString("categorie"),
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
所以我使用 AsyncTask 来恢复我的数据,然后我尝试在 Toast 中显示它。
我的 json 看起来像on PasteBin.
当我启动我的应用程序时,我的ProgressDialog关闭后会收到以下警告:
W/System.err(13382): org.json.JSONException: Value {"chambres":[{"adresse1":"23 allée des Fontainiers","adresse2":"BP 125","fax":"04 92 32 17 75","codePostal":"04004","email":"chmetiers@cm-04.fr","ndepartement":"04","tel":"04 92 30 90 90","ville":"DIGNE-LES-BAINS","urlSite":"http:\/\/www.cm-04.fr","nom":"CMA des Alpes-de-Haute-Provence"},{"adresse1":"Square Voltaire","adresse2":"","fax":"04 92 53 48 85","codePostal":"05000","email":"chambremetiers@cma05.fr","ndepartement":"05","tel":"04 92 51 06 89","ville":"GAP","urlSite":"http:\/\/www.cm-gap.fr","nom":"CMA des Hautes-Alpes"},{"adresse1":"110, avenue de Verdun","adresse2":"","fax":"04 93 31 07 56","codePostal":"06700","email":"direction@cma06.fr","ndepartement":"06","tel":"04 93 14 16 14","ville":"ST-LAURENT-DU-VAR","urlSite":"http:\/\/www.cm-alpesmaritimes.fr","nom":"CMA des Alpes-Maritimes"},{"adresse1":"8 rue de Clèves","adresse2":"BP 290","fax":"03 24 56 81 88","codePostal":"08014","email":"cma@cma-ardennes.fr","ndepartement":"08","tel":"03 24 56 81 81","ville":"CHARLEVILLE-MEZIERES","urlSite":"http:\/\/www.cma-ardennes.fr","nom":"CMA des Ardennes"},{"adresse1":"5 boulevard Pèbre","adresse2":"","fax":"04 91 32 24 25","codePostal":"13295","email":"cm13@cm13.org","ndepartement":"13","tel":"04 91 32 24 24","ville":"MARSEILLE","urlSite":"http:\/\/www.cma13.fr","nom":"CMA des Bouches du Rhône"},{"adresse1":"2 rue Claude-Bloch","adresse2":"BP 5059","fax":"02 31 53 25 01","codePostal":"14077","email":"contact14@cmar-bn.fr","ndepartement":"14","tel":"02 31 53 25 00","ville":"CAEN","urlSite":"http:\/\/www.cmar-bn.fr","nom":"CMAR de Basse-Normandie : section du Calvados"},{"adresse1":"45 avenue de la République","adresse2":"BP 511","fax":"04 71 45 65 10","codePostal":"15005","email":"sylvie.meinier@cma-cantal.fr","ndepartement":"15","tel":"04 71 45 65 00","ville":"AURILLAC","urlSite":"http:\/\/www.cma-cantal.fr","nom":"CMA du Cantal"},{"adresse1":"68 avenue Gambetta","adresse2":"","fax":"05 45 90 47 29","codePostal":"16021","email":"accueil@cma-charente.fr","ndepartement":"16","tel":"05 45 90 47 00","ville":"ANGOULEME","urlSite":"http:\/\/www.cma-charente.fr","nom":"CMA de la Charente"},{"adresse1":"107, avenue Michel-Crépeau","adresse2":"","fax":"05 46 50 00 09","codePostal":"17024","email":"cm17@cm-larochelle.fr","ndepartement":"17","tel":"05 46 50 00 00","ville":"LA ROCHELLE","urlSite":"http:\/\/www.cm-larochelle.fr","nom":"CMA de la Charente-Maritime"},{"adresse1":"40 rue Moyenne","adresse2":"BP 249","fax":"02 48 69 70 69","codePostal":"18005","email":"cm18@cm-bourges.fr","ndepartement":"18","tel":"02 48 69 70 71","ville":"BOURGES","urlSite":"http:\/\/www.cm-18.fr","nom":"CMA du Cher"},{"adresse1":"Immeuble Jean-Marie-Saute","adresse2":"8 avenue Alsace-Lorraine","fax":"05 55 20 19 14","codePostal":"19002","email":"artisanat@cma-correze.fr","ndepartement":"19","tel":"05 55 29 95 95","ville":"TULLE","urlSite":"http:\/\/www.cma-correze.fr","nom":"CMA de la Corrèze"},{"adresse1":"5-7 rue de Londres","adresse2":"BP 49","fax":"05 55 51 95 39","codePostal":"23011","email":"contact@cma-gueret.fr","ndepartement":"23","tel":"05 55 51 95 30","ville":"GUERET","urlSite":"http:\/\/www.crma-limousin.fr","nom":"CMA de la Creuse"},{"adresse1":"Créavallée Nord","adresse2":"Boulevard des saveurs","fax":"05 53 45 15 08","codePostal":"24660","email":"conseil@cm24.fr","ndepartement":"24","tel":"05 53 35 87 00","ville":"COULOUNIEIX CHAMIERS","urlSite":"http:\/\/www.artisanat24.com\/","nom":"CMA de la Dordogne"},{"adresse1":"21 rue de la Préfecture","adresse2":"BP 75","fax":"03 81 21 35 00","codePostal":"25013","email":"contact25@artisanat-comtois.fr","ndepartement":"25","tel":"03 81 21 35 35","ville":"BESANCON","urlSite":"http:\/\/www.cma-doubs.fr","nom":"CMA du Doubs"},{"adresse1":"Avenue Adolphe-Figuet","adresse2":"BP 153-Le clos des Tanneurs","fax":"04 75 02 73 94","codePostal":"26104","email":"c.francois@cma-drome.fr","ndepartement":"26","tel":"04 75 48 72 00","ville":"ROMANS","urlSite":"http:\/\/www.cma-drome.fr","nom":"CMA de la Drôme"},{"adres
W/System.err(13382): at org.json.JSON.typeMismatch(JSON.java:111)
W/System.err(13382): at org.json.JSONArray.<init>(JSONArray.java:91)
W/System.err(13382): at org.json.JSONArray.<init>(JSONArray.java:103)
...
它没有任何东西。我试图验证json,并且明显没有错误。所以我想知道你是否知道它可以来自哪里。
答案 0 :(得分:3)
我认为您的问题是您立即尝试解析JSON
数组:
尝试这样做:
JSONObject json = new JSONObject(result);
然后开始解析chambres
数组:
JSONArray array = json.getJSONArray("chambres");
依旧......
答案 1 :(得分:1)
您正在为JSONArray
构造函数提供此值:{"chambres":[{"adresse1":"23....
但这不是JSON数组,而是对象。