net.sf.json - JSONArray.fromObject

2021-06-06 04:02

阅读:673

标签:string   ring   put   util   rgs   tostring   map   valueof   void   

net.sf.json - JSONArray.fromObject

package com.ikoo;

import java.util.ArrayList;
import java.util.HashMap;

import net.sf.json.JSONArray;

public class Test {
    public static void main(String[] args) {
        ArrayList> list = new ArrayList();
        for (int i = 0; i ) {
            HashMap map = new HashMap();
            map.put("num", String.valueOf(i * 10));
            list.add(map);
        }
        System.out.println(list);// [{num=0}, {num=10}, {num=20}]
        JSONArray jsonArray = JSONArray.fromObject(list);
        System.out.println(jsonArray);// [{"num":"0"},{"num":"10"},{"num":"20"}]
        String data = "{\"success\":true,\"nums\":" + jsonArray.toString() + "}";
        System.out.println(data);// {"success":true,"nums":[{"num":"0"},{"num":"10"},{"num":"20"}]}
    }
}

将 jsonArray 再自定义组装的 json

{
  "success":true,
  "nums":[
    {
      "num": "0"
    },
    {
      "num": "10"
    },
    {
      "num": "20"
    }
  ]
}

 

net.sf.json - JSONArray.fromObject

标签:string   ring   put   util   rgs   tostring   map   valueof   void   

原文地址:http://www.cnblogs.com/ikoo4396/p/7339868.html


评论


亲,登录后才可以留言!