我有一个json
响应,如下所示
[
{
"id": "1",
"name": "b day",
"date": "2015-12-08",
"start_time": "00:50:02",
"end_time": "05:00:00"
},
{
"id": "2",
"name": "game",
"date": "2015-11-18",
"start_time": "00:00:02",
"end_time": "09:10:00"
}
]
我的Android代码检索json
如下所示
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
//success
BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
}
答案 0 :(得分:0)
您可以获得如下所示的JSONArray
。
JSONArray jsonArray = new JSONArray(responseString);
从数组中,您可以获得JSONObject
,如下所示。
for(int i = 0 ; i< jsonArray.length() ; i++){
// This will get first JSONObject from JSONArray.
JSONObject jObject = jsonArray.getJSONObject(i);
// Get all key from array using JSONObject.
String id = jObject.getString("id");
String name = jObject.getString("name");
String date = jObject.getString("date");
String startTime = jObject.getString("start_time");
String endTime = jObject.getString("end_time");
}
答案 1 :(得分:0)
试试这个:
try
{
JSONObject issueObj = new JSONObject(JSONString);
Iterator iterator = issueObj.keys();
while (iterator.hasNext())
{
String key = (String) iterator.next();
JSONObject issue = issueObj.getJSONObject(key);
int pubKey = Integer.valueOf(issue.optString("id"));
mylist.add(pubKey);
JSONObject json = new JSONObject(JSONString);
JSONObject jArray = json.getJSONObject(String.valueOf(pubKey));
nameString = jArray.getString("name");
dateString = jArray.getString("date");
start_timeString = jArray.getString("start_time");
end_timeString = jArray.getString("end_time");
}
} catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 2 :(得分:0)
Use this code
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
}
json = response.toString();
JSONArray jsonArray = new JSONArray(json);
ArrayList<String> al_ids=new ArrayList<String>();
Arraylist<String> al_name=new ArrayList<String>();
Arraylist<String> al_date=new ArrayList<String>();
Arraylist<String> al_start_time=new ArrayList<String>();
Arraylist<String> al_end_time=new ArrayList<String>();
for(int i = 0 ; i< jsonArray.length() ; i++){
// This will get first JSONObject from JSONArray.
JSONObject jObject = jsonArray.getJSONObject(i);
// Get all key from array using JSONObject.
String id = jObject.getString("id");
String name = jObject.getString("name");
String date = jObject.getString("date");
String startTime = jObject.getString("start_time");
String endTime = jObject.getString("end_time");
al_ids.add(id);
al_name.add(name);
al_date.add(date);
al_start_time.add(startTime);
al_end_time.add(endTime);
}
//here you can set text to Your TextView by getting position of arraylist