【问题标题】:Java HttpsURLConnection response JSON arrayJava HttpsURLConnection 响应 JSON 数组
【发布时间】:2016-09-22 18:29:04
【问题描述】:
我一直在接收 JSON 对象作为来自 HttpsURLConnection 的响应。我一直在使用它来解析响应。
ObjectMapper map = new ObjectMapper();
JsonNode node = map.readTree(conn.getInputStream());
这对我来说一直很好,但现在我正在接收数组。我该如何解析它们?
这是我收到的响应示例:
"value":
[1
]
0:
{
"ID": "2135125324"
"name": "John"
"2ndName": null
"lastName": "James"
}
【问题讨论】:
标签:
java
android
arrays
json
【解决方案1】:
如果您正在使用 AsyncTask,请尝试此操作
写下面的代码对你有帮助
private void yourfunction()
{
class YourClass extends AsyncTask<String, Void, String>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONObject jsonObj = new JSONObject(s);
user = jsonObj.getJSONArray("value");
JSONObject c = user.getJSONObject(0);
String profile = c.getString("ID");
String name = c.getString("name");
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(String... params) {
String s = params[0];
BufferedReader bufferedReader = null;
try {
URL url = new URL("your url string");
HttpURLConnection con= (HttpURLConnection) url.openConnection();
bufferedReader=new BufferedReader(new InputStreamReader(con.getInputStream()));
String result;
result = bufferedReader.readLine();
return result;
}
catch (Exception e) {
return null;
}
}
}
YourClass lu=new YourClass();
lu.execute();
}