我有一個JSON文件,我想解析但不知道如何正確訪問它。Android Studio:JSON解析
它不以一個對象括號「{」開頭,之後有一個名稱,例如, 「演員:」「[」....]}
我可以輕鬆創建一個 JSONObject jObj = new JSONObject(data); JSONArray jArray = jObj.getJSONArray(「actors」);
礦山看起來更像這個
[
{
"type": "fuel",
"name": "Aral",
"address": "Somestreet 65",
"lat": 49.8848387,
"lon": 8.6520691 },
{
"type": "amenity",
"name": "Centralstation",
"address": "Centralstreet 20",
"lat": 49.8725,
"lon": 8.628889,
"icon": "somepicture.jpg" },
]
我想是這樣
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
if(status == 200){
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONArray jsonArray = new JSONArray(data);
//JSONObject jsonObject = new JSONObject(data);
for(int i=0; i< jsonArray.length();i++){
Locations location = new Locations();
JSONObject jRealObject = jsonArray.getJSONObject(i);
location.setName(jRealObject.getString("type"));
location.setName(jRealObject.getString("name"));
location.setName(jRealObject.getString("address"));
location.setName(jRealObject.getString("lat"));
location.setName(jRealObject.getString("lon"));
//location.setImage(jRealObject.getString("icon"));
locationList.add(location);
}
return true;
}
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}catch (JSONException e){
e.printStackTrace();
}
return false;
}
但有一個錯誤在解析它,我認爲它是與「JSONArray jsonArray =新JSONArray( 「」);
你能幫我一下,或者指點一下我能找到我的錯誤的方向嗎
初始化Json數組爲:JSONArray jasnArray = new JSONArray(); – Akshay
對我不起作用 –