我想从下到上对 json 进行排序,我可以从上到下解析 json,但我需要从下到上。
是否可以?我怎样才能做到这一点?
我可以使用以下代码解析 json:
主活动文件:
public class MainActivity extends AppCompatActivity {
private ListView lv;
private static String url = "https://api.androidhive.info/contacts/";
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(url);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray contacts = jsonObj.getJSONArray("contacts");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String name = c.getString("name");
String email = c.getString("email");
HashMap<String, String> contact = new HashMap<>();
contact.put("name", name);
contact.put("email", email);
contactList.add(contact);
}
} catch (final JSONException e) {
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.list_item, new String[]{"name", "email"}, new int[]{R.id.name,
R.id.email});
lv.setAdapter(adapter);
}
}
}
HttpHandler 文件:
public class HttpHandler {
public String makeServiceCall(String reqUrl) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (Exception e) {
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
json文件:
{
"contacts": [
{
"name": "leonardo",
"email": "[email protected]"
}
},
{
"name": "Johnny",
"email": "[email protected]"
}
},
{
"name": "jack",
"email": "[email protected]"
}
}
{
"name": "paul",
"email": "[email protected]"
}
}
]
}
输出:
leonardo - [email protected]
-----------------------------
johnny - [email protected]
-----------------------------
jack - [email protected]
-----------------------------
paul - [email protected]
但首先我想像这样显示最后一个项目:
paul - [email protected]
-----------------------------
jack - [email protected]
-----------------------------
johnny - [email protected]
-----------------------------
leonardo - [email protected]
简单的 make 循环反向:
for (int i = contacts.length()-1; i>=0; i--)
{
JSONObject c = contacts.getJSONObject(i);
String name = c.getString("name");
String email = c.getString("email");
HashMap<String, String> contact = new HashMap<>();
contact.put("name", name);
contact.put("email", email);
contactList.add(contact);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句