JsonObject&JsonArray拼接

207 阅读1分钟

代码加持!Bug遁形!

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
	
//JSONArray里putJSONObject
JSONArray jsonArray = new JSONArray();
JSONObject json = new JSONObject();
json.put("test", "123");
jsonArray.put(json);

//putJSONObject里JSONArray
JSONArray arrays = new JSONArray();
json.put("arrays", arrays);

//JSONObject中put List
List<String> list = new ArrayList<>();
list.add("07/31/21");
JSONObject formResponseV1 = new JSONObject();
formResponseV1.put("list",list);

//JSONArray转字符串
String Json = jsonArray.toString();

//JSONObject中的toString(int indentFactor)
json.toString(1);
//参数是换行值