net.sf.json - JSONArray.fromObject
Posted 你的笑忘书
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了net.sf.json - JSONArray.fromObject相关的知识,希望对你有一定的参考价值。
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<HashMap<String, String>> list = new ArrayList<>(); for (int i = 0; i < 3; i++) { HashMap<String, String> 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的主要内容,如果未能解决你的问题,请参考以下文章
net.sf.json.JSON把JSON纯数组形式转为List
急!ssh+json 空指针异常(net.sf.json.JSONException: java.lang.NullPointerException)
net.sf.json - JSONArray.fromObject