整理json数据格式的问题

把后台给我的 goods_name 数据 弄成像下面的数组 该怎么办
图片说明

我用的是拼接 感觉很麻烦

图片说明

直接用map处理一下就行了吧

<script type="text/javascript">
var a=[{"id":1,"goods_name":"test1"},{"id":2,"goods_name":"test2"},{"id":3,"goods_name":"test3"},{"id":4,"goods_name":"test4"}];

var m=$(a).map(function() {
            console.log(this.goods_name)
            return this.goods_name;
}).get().join(',');

    console.log(m);
</script>

直接后台把查询出来的数据打包成json,传前台就行。不需要前台拼接。

1. List集合转换成json代码
  List list = new ArrayList();
  list.add( "first" );
  list.add( "second" );
  JSONArray jsonArray2 = JSONArray.fromObject( list );
2. Map集合转换成json代码
  Map map = new HashMap();
  map.put("name", "json");
  map.put("bool", Boolean.TRUE);
  map.put("int", new Integer(1));
  map.put("arr", new String[] { "a", "b" });
  map.put("func", "function(i){ return this.arr[i]; }");
  JSONObject json = JSONObject.fromObject(map);
3. Bean转换成json代码
  JSONObject jsonObject = JSONObject.fromObject(new JsonBean());
4. 数组转换成json代码
  boolean[] boolArray = new boolean[] { true, false, true };
  JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
5. 一般数据转换成json代码
  JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );

用forEach循环遍历 然后添加到一个新数组里