【发布时间】:2017-11-04 11:01:25
【问题描述】:
我目前正在尝试解析一个简单的JSON 信息,但无法找出JSON 对象和数组部分...我正在尝试从JSON(下方)中提取app_time 和邮政编码+ 地址。谁能给我一个关于我的“extractFeatureFromJson()”的解决方案,对不起格式,这是我在这里的第一篇文章。
{
"data": [
{
"id": 24256,
"app_time": 1904280242,
"addresses": [
{
"id": 1,
"postcode": "9000",
"address": "Street:Street, City: City, Country: Country"
}
],
"comments": [
{
"id": 1,
"comment": "Comment",
"created_at": 234234234
}
]
}
]
}
public static final String LOG_TAG = MainActivity.class.getSimpleName();
private static final String _URL = "https://.......com/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ScheduleAsyncTask task = new ScheduleAsyncTask();
task.execute();
}
private void updateUi(Event job) {
TextView titleTextView = (TextView) findViewById(R.id.time);
titleTextView.setText(getDateString(job.time));
TextView dateTextView = (TextView) findViewById(R.id.address);
dateTextView.setText(job.address);
}
private String getDateString(long timeInMilliseconds) {
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
return formatter.format(timeInMilliseconds);
}
private class ScheduleAsyncTask extends AsyncTask<URL, Void, Event> {
@Override
protected Event doInBackground(URL... urls) {
// Create URL object
URL url = createUrl(_URL);
// Perform HTTP request to the URL and receive a JSON response back
String jsonResponse = "";
try {
jsonResponse = makeHttpRequest(url);
} catch (IOException e) {
// TODO Handle the IOException
}
Event jobs = extractFeatureFromJson(jsonResponse);
return jobs;
}
@Override
protected void onPostExecute(Event job) {
if (job == null) {
return;
}
updateUi(job);
}
/**
* Returns new URL object from the given string URL.
*/
private URL createUrl(String stringUrl) {
URL url = null;
try {
url = new URL(stringUrl);
} catch (MalformedURLException exception) {
Log.e(LOG_TAG, "Error with creating URL", exception);
return null;
}
return url;
}
private String makeHttpRequest(URL url) throws IOException {
String jsonResponse = "";
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("X-Application", ".....");
urlConnection.connect();
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
} catch (IOException e) {
// TODO: Handle the exception
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
// function must handle java.io.IOException here
inputStream.close();
}
}
return jsonResponse;
}
private String readFromStream(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
}
private Event extractFeatureFromJson(String scheduleJSON) {
try {
JSONObject baseJsonResponse = new JSONObject(scheduleJSON);
JSONArray featureArray = baseJsonResponse.getJSONArray("comments");
// If there are results in the features array
// Extract out the first feature
JSONObject firstFeature = featureArray.getJSONObject(0);
JSONObject properties = firstFeature.getJSONObject("comment");
// Extract out the time address values
String address = properties.getString("address");
long time = properties.getLong("app_time");
// Create a new {@link Event} object
return new Event(address, time);
} catch (JSONException e) {
Log.e(LOG_TAG, "Problem parsing the JSON results", e);
}
return null;
}
}
}
【问题讨论】: