Search in sources :

Example 26 with JSONObject

use of com.alibaba.fastjson.JSONObject in project wechat-mp-sdk by usc.

the class MenuUtil method buildMenu.

private static Menu buildMenu(String json) {
    JSONObject parseObject = JSONObject.parseObject(json);
    if (json == null) {
        return null;
    }
    JSONObject menuObject = parseObject.getJSONObject("menu");
    if (menuObject == null) {
        return null;
    }
    JSONArray jsonArray = menuObject.getJSONArray("button");
    if (jsonArray == null) {
        return null;
    }
    List<MenuInfo> menuInfos = new ArrayList<MenuInfo>();
    for (int i = 0; i < jsonArray.size(); i++) {
        JSONObject subJsonObject = jsonArray.getJSONObject(i);
        String type = subJsonObject.getString("type");
        String jsonString = subJsonObject.toString();
        if (StringUtils.isNotEmpty(type)) {
            menuInfos.add(JSONObject.parseObject(jsonString, SingleMenuInfo.class));
        } else {
            menuInfos.add(JSONObject.parseObject(jsonString, MultiMenuInfo.class));
        }
    }
    return new Menu(menuInfos);
}
Also used : MultiMenuInfo(org.usc.wechat.mp.sdk.vo.menu.MultiMenuInfo) JSONObject(com.alibaba.fastjson.JSONObject) SingleMenuInfo(org.usc.wechat.mp.sdk.vo.menu.SingleMenuInfo) MultiMenuInfo(org.usc.wechat.mp.sdk.vo.menu.MultiMenuInfo) MenuInfo(org.usc.wechat.mp.sdk.vo.menu.MenuInfo) SingleMenuInfo(org.usc.wechat.mp.sdk.vo.menu.SingleMenuInfo) JSONArray(com.alibaba.fastjson.JSONArray) ArrayList(java.util.ArrayList) Menu(org.usc.wechat.mp.sdk.vo.menu.Menu)

Example 27 with JSONObject

use of com.alibaba.fastjson.JSONObject in project uavstack by uavorg.

the class SMSAction method run.

@Override
public boolean run(NotificationEvent notifyEvent) {
    // 模板关键字
    Map<String, String> keywords = new HashMap<String, String>();
    String title = notifyEvent.getTitle();
    long timeFlag = notifyEvent.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String sd = sdf.format(new Date(timeFlag));
    keywords.put("custName", sd);
    keywords.put("comName", title);
    /**
     * 若notifyEvent中包含目的地址,则用该地址;否则使用默认地址
     */
    String sms = notifyEvent.getArg(cName);
    if (StringHelper.isEmpty(sms)) {
        if (log.isTraceEnable()) {
            log.warn(this, "Send SMS FAIL as no any phone numbers.");
        }
        return false;
    }
    String[] phoneNums = sms.split(",");
    for (String phone : phoneNums) {
        List<ParaValuePair> nvps = new ArrayList<ParaValuePair>();
        // 接口版本
        nvps.add(new ParaValuePair("version", "3.0"));
        // 批次号
        nvps.add(new ParaValuePair("batchId", Long.toString(System.currentTimeMillis())));
        // 组织机构号
        nvps.add(new ParaValuePair("orgNo", "2265"));
        // 模板号
        nvps.add(new ParaValuePair("typeNo", "7209"));
        // 关键字替换
        nvps.add(new ParaValuePair("keywords", JSON.toJSONString(keywords)));
        // 手机号
        nvps.add(new ParaValuePair("mobile", phone));
        if (log.isDebugEnable()) {
            log.debug(this, "Send SMS START: phone=" + phone);
        }
        final String phoneNum = phone;
        client.doAsyncHttpPost(baseUrl + "send", nvps, "utf-8", new HttpClientCallback() {

            @Override
            public void completed(HttpClientCallbackResult result) {
                String results = result.getReplyDataAsString();
                JSONObject jo = JSON.parseObject(results);
                // 错误码
                String code = jo.getString("code");
                // 错误信息
                String desc = jo.getString("desc");
                if (log.isDebugEnable()) {
                    log.debug(this, "Send SMS END: phone=" + phoneNum + ",code=" + code + ",desc=" + desc);
                }
            }

            @Override
            public void failed(HttpClientCallbackResult result) {
                String results = result.getReplyDataAsString();
                log.err(this, "Send SMS FAIL: phone=" + phoneNum + ", result=" + results);
            }
        });
    }
    return true;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Date(java.sql.Date) HttpClientCallback(com.creditease.uav.httpasync.HttpClientCallback) ParaValuePair(com.creditease.uav.httpasync.api.ParaValuePair) JSONObject(com.alibaba.fastjson.JSONObject) HttpClientCallbackResult(com.creditease.uav.httpasync.HttpClientCallbackResult) SimpleDateFormat(java.text.SimpleDateFormat)

Example 28 with JSONObject

use of com.alibaba.fastjson.JSONObject in project uavstack by uavorg.

the class TaildirLogComponent method writePosition.

private void writePosition(boolean isAppend) {
    File file = new File(positionFilePath);
    LineNumberReader reader = null;
    FileWriter writer = null;
    @SuppressWarnings("rawtypes") List<Map> json = Lists.newArrayList();
    if (!existingInodes.isEmpty()) {
        json.addAll(toPosInfoJson());
    }
    try {
        reader = new LineNumberReader(new FileReader(file));
        JSONArray array = Optional.fromNullable(JSONObject.parseArray(reader.readLine())).or(new JSONArray());
        for (int i = 0; i < array.size(); i++) {
            JSONObject posfile = array.getJSONObject(i);
            if (!existingInodes.contains(posfile.getLongValue("inode")))
                json.add(ImmutableMap.of("inode", posfile.getLongValue("inode"), "pos", posfile.getLongValue("pos"), "num", posfile.getLong("num"), "file", posfile.getString("file")));
        }
        writer = new FileWriter(file);
        writer.write(JSONObject.toJSONString(json));
    } catch (Throwable t) {
        log.err(this, "Failed writing positionFile", t);
    } finally {
        try {
            if (reader != null)
                reader.close();
        } catch (IOException e) {
            log.err(this, "Error: " + e.getMessage(), e);
        }
        try {
            if (writer != null)
                writer.close();
        } catch (IOException e) {
            log.err(this, "Error: " + e.getMessage(), e);
        }
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) FileWriter(java.io.FileWriter) JSONArray(com.alibaba.fastjson.JSONArray) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LineNumberReader(java.io.LineNumberReader)

Example 29 with JSONObject

use of com.alibaba.fastjson.JSONObject in project uavstack by uavorg.

the class DefaultJSONParser method parseArray.

@SuppressWarnings({ "unchecked", "rawtypes" })
public final void parseArray(final Collection array, Object fieldName) {
    final JSONLexer lexer = getLexer();
    if (lexer.token() == JSONToken.SET || lexer.token() == JSONToken.TREE_SET) {
        lexer.nextToken();
    }
    if (lexer.token() != JSONToken.LBRACKET) {
        throw new JSONException("syntax error, expect [, actual " + JSONToken.name(lexer.token()) + ", pos " + lexer.pos());
    }
    lexer.nextToken(JSONToken.LITERAL_STRING);
    ParseContext context = this.getContext();
    this.setContext(array, fieldName);
    try {
        for (int i = 0; ; ++i) {
            if (isEnabled(Feature.AllowArbitraryCommas)) {
                while (lexer.token() == JSONToken.COMMA) {
                    lexer.nextToken();
                    continue;
                }
            }
            Object value;
            switch(lexer.token()) {
                case LITERAL_INT:
                    value = lexer.integerValue();
                    lexer.nextToken(JSONToken.COMMA);
                    break;
                case LITERAL_FLOAT:
                    if (lexer.isEnabled(Feature.UseBigDecimal)) {
                        value = lexer.decimalValue(true);
                    } else {
                        value = lexer.decimalValue(false);
                    }
                    lexer.nextToken(JSONToken.COMMA);
                    break;
                case LITERAL_STRING:
                    String stringLiteral = lexer.stringVal();
                    lexer.nextToken(JSONToken.COMMA);
                    if (lexer.isEnabled(Feature.AllowISO8601DateFormat)) {
                        JSONScanner iso8601Lexer = new JSONScanner(stringLiteral);
                        if (iso8601Lexer.scanISO8601DateIfMatch()) {
                            value = iso8601Lexer.getCalendar().getTime();
                        } else {
                            value = stringLiteral;
                        }
                        iso8601Lexer.close();
                    } else {
                        value = stringLiteral;
                    }
                    break;
                case TRUE:
                    value = Boolean.TRUE;
                    lexer.nextToken(JSONToken.COMMA);
                    break;
                case FALSE:
                    value = Boolean.FALSE;
                    lexer.nextToken(JSONToken.COMMA);
                    break;
                case LBRACE:
                    JSONObject object = new JSONObject(isEnabled(Feature.OrderedField));
                    value = parseObject(object, i);
                    break;
                case LBRACKET:
                    Collection items = new JSONArray();
                    parseArray(items, i);
                    value = items;
                    break;
                case NULL:
                    value = null;
                    lexer.nextToken(JSONToken.LITERAL_STRING);
                    break;
                case UNDEFINED:
                    value = null;
                    lexer.nextToken(JSONToken.LITERAL_STRING);
                    break;
                case RBRACKET:
                    lexer.nextToken(JSONToken.COMMA);
                    return;
                case EOF:
                    throw new JSONException("unclosed jsonArray");
                default:
                    value = parse();
                    break;
            }
            array.add(value);
            checkListResolve(array);
            if (lexer.token() == JSONToken.COMMA) {
                lexer.nextToken(JSONToken.LITERAL_STRING);
                continue;
            }
        }
    } finally {
        this.setContext(context);
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) JSONException(com.alibaba.fastjson.JSONException) Collection(java.util.Collection) JSONObject(com.alibaba.fastjson.JSONObject)

Example 30 with JSONObject

use of com.alibaba.fastjson.JSONObject in project uavstack by uavorg.

the class DefaultJSONParser method parseObject.

public JSONObject parseObject() {
    JSONObject object = new JSONObject(isEnabled(Feature.OrderedField));
    parseObject(object);
    return object;
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)1750 HashMap (java.util.HashMap)302 JSONArray (com.alibaba.fastjson.JSONArray)279 Test (org.junit.Test)217 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)182 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)165 IOException (java.io.IOException)138 ArrayList (java.util.ArrayList)125 Map (java.util.Map)111 AuthPassport (com.ngtesting.platform.util.AuthPassport)96 UserVo (com.ngtesting.platform.vo.UserVo)76 Date (java.util.Date)76 List (java.util.List)72 File (java.io.File)70 UnitResponse (info.xiancloud.core.message.UnitResponse)61 JSONException (com.alibaba.fastjson.JSONException)46 StringEntity (org.apache.http.entity.StringEntity)46 HttpResponse (org.apache.http.HttpResponse)44 Header (org.apache.http.Header)36 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)30