Search in sources :

Example 1 with JSONArray

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());
}
Also used : JSONArray(cn.hutool.json.JSONArray) Exam(cn.hutool.json.test.bean.Exam) Test(org.junit.Test)

Example 2 with JSONArray

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());
}
Also used : JSONArray(cn.hutool.json.JSONArray) Test(org.junit.Test)

Example 3 with JSONArray

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");
}
Also used : JSONArray(cn.hutool.json.JSONArray) Test(org.junit.Test)

Example 4 with JSONArray

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);
}
Also used : JSONObject(cn.hutool.json.JSONObject) JSONArray(cn.hutool.json.JSONArray) JSONObject(cn.hutool.json.JSONObject)

Example 5 with JSONArray

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);
}
Also used : JSONObject(cn.hutool.json.JSONObject) JSONArray(cn.hutool.json.JSONArray) JSONObject(cn.hutool.json.JSONObject)

Aggregations

JSONArray (cn.hutool.json.JSONArray)7 Test (org.junit.Test)4 JSONObject (cn.hutool.json.JSONObject)3 CollUtil (cn.hutool.core.collection.CollUtil)1 DateTime (cn.hutool.core.date.DateTime)1 DateUtil (cn.hutool.core.date.DateUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 Exam (cn.hutool.json.test.bean.Exam)1 Retry (com.hsn.epic4j.aop.Retry)1 Item (com.hsn.epic4j.bean.Item)1 SelectItem (com.hsn.epic4j.bean.SelectItem)1 EpicConfig (com.hsn.epic4j.config.EpicConfig)1 ItemException (com.hsn.epic4j.exception.ItemException)1 PermissionException (com.hsn.epic4j.exception.PermissionException)1 TimeException (com.hsn.epic4j.exception.TimeException)1 PageUtil (com.hsn.epic4j.util.PageUtil)1 Constant (com.ruiyun.jvppeteer.core.Constant)1 Puppeteer (com.ruiyun.jvppeteer.core.Puppeteer)1 Browser (com.ruiyun.jvppeteer.core.browser.Browser)1 BrowserFetcher (com.ruiyun.jvppeteer.core.browser.BrowserFetcher)1