use of cn.hutool.json.JSONArray in project hutool by looly.
the class JSONArrayTest method toListTest.
@Test
public void toListTest() {
String jsonStr = FileUtil.readString("exam_test.json", CharsetUtil.CHARSET_UTF_8);
JSONArray array = JSONUtil.parseArray(jsonStr);
ArrayList<Exam> list = array.toList(Exam.class);
Assert.assertFalse(list.isEmpty());
}
use of cn.hutool.json.JSONArray in project hutool by looly.
the class JSONArrayTest method toListWithNullTest.
/**
* 单元测试用于测试在列表元素中有null时的情况下是否出错
*/
@Test
public void toListWithNullTest() {
String json = "[null,{'akey':'avalue','bkey':'bvalue'}]";
JSONArray ja = JSONUtil.parseArray(json);
ArrayList<KeyBean> list = ja.toList(KeyBean.class);
Assert.assertTrue(null == list.get(0));
Assert.assertEquals("avalue", list.get(1).getAkey());
Assert.assertEquals("bvalue", list.get(1).getBkey());
}
use of cn.hutool.json.JSONArray in project hutool by looly.
the class JSONArrayTest method addTest.
@Test
public void addTest() {
// 方法1
JSONArray array = JSONUtil.createArray();
// 方法2
// JSONArray array = new JSONArray();
array.add("value1");
array.add("value2");
array.add("value3");
Assert.assertEquals(array.get(0), "value1");
}
use of cn.hutool.json.JSONArray in project hutool by looly.
the class JSONXMLSerializer method toXml.
/**
* 转换JSONObject为XML
*
* @param object JSON对象或数组
* @param tagName 可选标签名称,名称为空时忽略标签
* @param contentKeys 标识为内容的key,遇到此key直接解析内容而不增加对应名称标签
* @return A string.
* @throws JSONException JSON解析异常
*/
public static String toXml(Object object, String tagName, String... contentKeys) throws JSONException {
if (null == object) {
return null;
}
final StringBuilder sb = new StringBuilder();
if (object instanceof JSONObject) {
// Emit <tagName>
appendTag(sb, tagName, false);
// Loop thru the keys.
((JSONObject) object).forEach((key, value) -> {
if (ArrayUtil.isArray(value)) {
value = new JSONArray(value);
}
// Emit content in body
if (ArrayUtil.contains(contentKeys, key)) {
if (value instanceof JSONArray) {
int i = 0;
for (Object val : (JSONArray) value) {
if (i > 0) {
sb.append(CharUtil.LF);
}
sb.append(EscapeUtil.escapeXml(val.toString()));
i++;
}
} else {
sb.append(EscapeUtil.escapeXml(value.toString()));
}
// Emit an array of similar keys
} else if (StrUtil.isEmptyIfStr(value)) {
sb.append(wrapWithTag(null, key));
} else if (value instanceof JSONArray) {
for (Object val : (JSONArray) value) {
if (val instanceof JSONArray) {
sb.append(wrapWithTag(toXml(val), key));
} else {
sb.append(toXml(val, key));
}
}
} else {
sb.append(toXml(value, key));
}
});
// Emit the </tagname> close tag
appendTag(sb, tagName, true);
return sb.toString();
}
if (ArrayUtil.isArray(object)) {
object = new JSONArray(object);
}
if (object instanceof JSONArray) {
for (Object val : (JSONArray) object) {
// XML does not have good support for arrays. If an array
// appears in a place where XML is lacking, synthesize an
// <array> element.
sb.append(toXml(val, tagName == null ? "array" : tagName));
}
return sb.toString();
}
return wrapWithTag(EscapeUtil.escapeXml(object.toString()), tagName);
}
use of cn.hutool.json.JSONArray in project hutool by dromara.
the class JSONXMLSerializer method toXml.
/**
* 转换JSONObject为XML
*
* @param object JSON对象或数组
* @param tagName 可选标签名称,名称为空时忽略标签
* @param contentKeys 标识为内容的key,遇到此key直接解析内容而不增加对应名称标签
* @return A string.
* @throws JSONException JSON解析异常
*/
public static String toXml(Object object, String tagName, String... contentKeys) throws JSONException {
if (null == object) {
return null;
}
final StringBuilder sb = new StringBuilder();
if (object instanceof JSONObject) {
// Emit <tagName>
appendTag(sb, tagName, false);
// Loop thru the keys.
((JSONObject) object).forEach((key, value) -> {
if (ArrayUtil.isArray(value)) {
value = new JSONArray(value);
}
// Emit content in body
if (ArrayUtil.contains(contentKeys, key)) {
if (value instanceof JSONArray) {
int i = 0;
for (Object val : (JSONArray) value) {
if (i > 0) {
sb.append(CharUtil.LF);
}
sb.append(EscapeUtil.escapeXml(val.toString()));
i++;
}
} else {
sb.append(EscapeUtil.escapeXml(value.toString()));
}
// Emit an array of similar keys
} else if (StrUtil.isEmptyIfStr(value)) {
sb.append(wrapWithTag(null, key));
} else if (value instanceof JSONArray) {
for (Object val : (JSONArray) value) {
if (val instanceof JSONArray) {
sb.append(wrapWithTag(toXml(val), key));
} else {
sb.append(toXml(val, key));
}
}
} else {
sb.append(toXml(value, key));
}
});
// Emit the </tagname> close tag
appendTag(sb, tagName, true);
return sb.toString();
}
if (ArrayUtil.isArray(object)) {
object = new JSONArray(object);
}
if (object instanceof JSONArray) {
for (Object val : (JSONArray) object) {
// XML does not have good support for arrays. If an array
// appears in a place where XML is lacking, synthesize an
// <array> element.
sb.append(toXml(val, tagName == null ? "array" : tagName));
}
return sb.toString();
}
return wrapWithTag(EscapeUtil.escapeXml(object.toString()), tagName);
}
Aggregations