ToStringUtils.java 1.3 KB
package com.cnlive.im.util;

import com.cnlive.im.mode.CNMessageModole;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author 陈硕
 * @time 2017/3/28  10:20
 * @desc ${TODD}
 */
public class ToStringUtils {



    public static String objToJsonString(CNMessageModole cnMessageModole) {

        // 初始化返回值
        String json = "str_empty";

        if (cnMessageModole == null) {
            return json;
        }

        StringBuilder buff = new StringBuilder();
        buff.append("{");


        buff.append("message");
        buff.append(":");
        buff.append("\"");
        buff.append(cnMessageModole.getMessage() == null ? "" : cnMessageModole.getMessage());
        buff.append("\"");
        buff.append(",");


        buff.append("type");
        buff.append(":");
        buff.append("\"");
        buff.append(cnMessageModole.getType() == null ? "" : cnMessageModole.getType());
        buff.append("\"");

        buff.append("}");
        json = buff.toString();





        return json;
    }






    public static CNMessageModole JsonStringToobj(String CNMessagejson) throws JSONException {


        JSONObject jsonObj = new JSONObject(CNMessagejson);
        return  new CNMessageModole(jsonObj.get("message").toString(), jsonObj.get("type").toString());




    }


}