【发布时间】:2015-05-27 06:23:40
【问题描述】:
嗨,在我的 android 应用程序中,我收到了来自服务器的 JSON 响应,它没有键值,现在我应该解析那个 JSON,并且应该将解析后的数据附加到一个微调器。如何做到这一点?这是我尝试过的
This is my JSON
[["Basic","Premium","svdsv","uymyimy"]]
String url = "http://www.thetaf.com/TAFCycleStation/get_plans.php";
try {
JSONArray data = new JSONArray(getJSONUrl(url));
List<String> list = new ArrayList<String>();
for(int i = 0; i < data.length(); i++)
{
JSONObject c = data.getJSONObject(i);
list.add(c.getString("0"));
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
plans.setAdapter(dataAdapter);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getJSONUrl(String url) {
StringBuilder str = 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) { // Download OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
【问题讨论】:
-
没有key值怎么可能是json呢?
-
你能把json显示一次吗
-
是的,向我们展示 json
-
@RanjitPati [["Basic","Premium","svdsv","uymyimy"]] 这是我的 JSON
-
AFAIK 它不是 JSON..