package
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.amazonaws.util.json.JSONObject;
ArrayList is an array, the corresponding format is [];
Map is a key, value pair, the corresponding format is {key=value}
jsonobject is also a key, value pair, and the corresponding format is {key:value}
Example 1 : There are Map key-value pairs in the ArrayList array. The example code is as follows
@Test
public void testUpsert() {
ArrayList<String> arrayList = new ArrayList<String>();
JSONObject jsonObject=new JSONObject();
ArrayList<String> arrayListNew = new ArrayList<String>();
Map<String, String> map=new HashMap<String, String>();
arrayList.add("The King of Heaven and Earth Tiger");
arrayList.add("Pagoda Town River Demon");
arrayList.add("pheasant boring");
arrayList.add("How can I go to Tianwang Mountain");
for(String elem:arrayList){
map.put("黑话", elem);
arrayListNew.add(map.toString());
}
System.out.println(arrayListNew);
}
}
The print result is as follows;
[{Black words = Heavenly King Covering Tiger}, {Black words = Pagoda Town River Demon}, {Black words = Pheasant boring head drill}, {Black words = How can I go to Tianwang Mountain}]
Example 2. There are jsonObject key-value pairs in the ArrayList array. The example code is as follows
@Test
public void testUpsert() throws JSONException {
ArrayList<String> arrayList = new ArrayList<String>();
JSONObject jsonObject=new JSONObject();
ArrayList<String> arrayListNew = new ArrayList<String>();
Map<String, String> map=new HashMap<String, String>();
arrayList.add("The King of Heaven and Earth Tiger");
arrayList.add("Pagoda Town River Demon");
arrayList.add("pheasant boring");
arrayList.add("How can I go to Tianwang Mountain");
for(String elem:arrayList){
jsonObject.put("黑话", elem);
arrayListNew.add(jsonObject.toString());
}
System.out.println(arrayListNew);
}
}
The print result is as follows;
[{"Black Talk":"Tianwang Covering Tiger"}, {"Black Talk":"Pagoda Town River Demon"}, {"Black Talk":"Pheasant Drilling"}, {"Black Talk":"How can I go to Tianwang Mountain" }]