引包
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.amazonaws.util.json.JSONObject;
ArrayList是数组,对应格式为[];
Map是key,value对,对应格式为{key=value}
jsonobject也为key,value对,对应格式为{key:value}
实例1:ArrayList数组里面是Map键值对,实例代码如下
@Test public void testUpsert() { ArrayListarrayList = new ArrayList (); JSONObject jsonObject=new JSONObject(); ArrayList arrayListNew = new ArrayList (); Map map=new HashMap (); arrayList.add("天王盖地虎"); arrayList.add("宝塔镇河妖"); arrayList.add("野鸡闷头钻"); arrayList.add("哪能上天王山"); for(String elem:arrayList){ map.put("黑话", elem); arrayListNew.add(map.toString()); } System.out.println(arrayListNew); } }
打印结果如下;
[{黑话=天王盖地虎}, {黑话=宝塔镇河妖}, {黑话=野鸡闷头钻}, {黑话=哪能上天王山}]
实例2.ArrayList数组里面是jsonObject键值对,实例代码如下
@Test public void testUpsert() throws JSONException { ArrayListarrayList = new ArrayList (); JSONObject jsonObject=new JSONObject(); ArrayList arrayListNew = new ArrayList (); Map map=new HashMap (); arrayList.add("天王盖地虎"); arrayList.add("宝塔镇河妖"); arrayList.add("野鸡闷头钻"); arrayList.add("哪能上天王山"); for(String elem:arrayList){ jsonObject.put("黑话", elem); arrayListNew.add(jsonObject.toString()); } System.out.println(arrayListNew); }}
打印结果如下;
[{"黑话":"天王盖地虎"}, {"黑话":"宝塔镇河妖"}, {"黑话":"野鸡闷头钻"}, {"黑话":"哪能上天王山"}]