1024programmer Blog Convert json object to string java_Convert java object to json string

Convert json object to string java_Convert java object to json string

package com.cjonline.foundation.util;

import java.lang.reflect.Field;

import java.math.BigDecimal;

import java.text.SimpleDateFormat;

import java.util.Collection;

import java.util.Date;

public class JsonUtils {

/** Default string format */

private static String dateformat = “yyyy-MM-dd hh:mm:ss”;

/**

* Get date string format

*

* @return

*/

public static String getDateformat() {

return dateformat;

}

/**

* Set date string format

*

* @param dateformat

*/

public void setDateformat(String dateformat) {

JsonUtils.dateformat = dateformat;

}

/**

* Get the attribute return type of the entity bean

*

* @param typeName

*Type name

* @param fieldValue

* Field value

* @return

*/

private static Object toType(Object fieldValue) {

Object result = “”;

if (fieldValue instanceof String) {

String value = (String) fieldValue;

if (value.contains(“\r\n”)) {

value = value.replaceAll(“\r\n”, “\\\\r\\\\n”);

}

result = “\”” + value + “\””;

} else if (fieldValue instanceof Number) {

result = fieldValue;

} else if (fieldValue instanceof Boolean) {

result = fieldValue;

} else if (fieldValue instanceof BigDecimal) {

result = fieldValue;

} else if (fieldValue instanceof Date) {

SimpleDateFormat sdf = new SimpleDateFormat(getDateformat());

result = “\”” + sdf.format(fieldValue) + “\””;

} else {

result = “\”” + “\””;

;

}

return result;

}

/**

* is to format a single entity bean into a json string

*

* @param obj

* Entity beans

* @return json string

* @throws IllegalAccessException

* @throws IllegalArgumentException

* @throws Exception

*/

public static String Object2JSON(Object obj, Object[] showfields) {

StringBuffer sb = new StringBuffer();

sb.append(“{“);

if (obj == null) {

return sb.append(“}”).toString();

}

Field[] fds = obj.getClass().getDeclaredFields();

for (int i = 0; i < fds.length; i++) {

fds[i].setAccessible(true);

String fieldName = fds[i].getName();

Object fieldValue = null;

try {

fieldValue = fds[i].get(obj);

} catch (Exception e) {

e.printStackTrace();

}

if (showfields == null) {

sb.append(“\”” + fieldName + “\””).append(“:”);

sb.append(toType(fieldValue)).append(“,”);

} else {

for (Object showfield : showfields) {

if (showfield instanceof String) {

if (fieldName.equalsIgnoreCase((String) showfield)) {

sb.append(“\”” + fieldName + “\””).append(“:”);

sb.append(toType(fieldValue)).append(“,”);

}

}

}

}

}

String result = “”;

if (sb.toString().length() == 1) {

result = “{“;

} else {

result = sb.substring(0, sb.length() – 1);

}

return result + “}”;

}

/**

* Can operate on a collection of multiple entity beans, and the output is in grid format

*

* @param obj

* Can be a collection or individual of entity beans

* @param showfields

* Fields that need to be displayed

* @return

* @throws Exception

*/

public static String ListObject2JSON(Object obj, Object[] showfields)

throws Exception {

StringBuffer sb = new StringBuffer();

StringBuffer rows = new StringBuffer();

sb.append(“[“);

if (obj instanceof Collection) {

@SuppressWarnings(“rawtypes”)

Collection cc = (Collection) obj;

if (cc.size() < 1) {

return sb.append(“]”).toString();

}

Object[] objects = cc.toArray();

for (Object object : objects) {

rows.append(Object2JSON(object, showfields)).append(“,”);

}

rows = rows.replace(rows.length() – 1, rows.length(), “”);

}

sb.append(rows).append(“]”);

return sb.toString();

}

}

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/convert-json-object-to-string-java_convert-java-object-to-json-string/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索