我一直在接收来自HttpsURLConnection的响应的JSON对象。我一直用它来解析响应。
ObjectMapper map = new ObjectMapper();
JsonNode node = map.readTree(conn.getInputStream());
这对我来说很好,但现在我正在接收数组。我该如何解析它们?
这是我收到的回复示例:
"value":
[1
]
0:
{
"ID": "2135125324"
"name": "John"
"2ndName": null
"lastName": "James"
}
答案 0 :(得分: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();
}