Search in sources :

Example 6 with JsonObject

use of com.google.gson.JsonObject in project weixin-java-tools by chanjarster.

the class WxCpServiceImpl method tagCreate.

@Override
public String tagCreate(String tagName) throws WxErrorException {
    String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/create";
    JsonObject o = new JsonObject();
    o.addProperty("tagname", tagName);
    String responseContent = post(url, o.toString());
    JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
    return tmpJsonElement.getAsJsonObject().get("tagid").getAsString();
}
Also used : JsonElement(com.google.gson.JsonElement) StringReader(java.io.StringReader) JsonObject(com.google.gson.JsonObject) JsonReader(com.google.gson.stream.JsonReader)

Example 7 with JsonObject

use of com.google.gson.JsonObject in project weixin-java-tools by chanjarster.

the class WxCpServiceImpl method userDelete.

@Override
public void userDelete(String[] userids) throws WxErrorException {
    String url = "https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete";
    JsonObject jsonObject = new JsonObject();
    JsonArray jsonArray = new JsonArray();
    for (int i = 0; i < userids.length; i++) {
        jsonArray.add(new JsonPrimitive(userids[i]));
    }
    jsonObject.add("useridlist", jsonArray);
    post(url, jsonObject.toString());
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject)

Example 8 with JsonObject

use of com.google.gson.JsonObject in project weixin-java-tools by chanjarster.

the class WxCpServiceImpl method tagAddUsers.

@Override
public void tagAddUsers(String tagId, List<String> userIds, List<String> partyIds) throws WxErrorException {
    String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers";
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("tagid", tagId);
    if (userIds != null) {
        JsonArray jsonArray = new JsonArray();
        for (String userId : userIds) {
            jsonArray.add(new JsonPrimitive(userId));
        }
        jsonObject.add("userlist", jsonArray);
    }
    if (partyIds != null) {
        JsonArray jsonArray = new JsonArray();
        for (String userId : partyIds) {
            jsonArray.add(new JsonPrimitive(userId));
        }
        jsonObject.add("partylist", jsonArray);
    }
    post(url, jsonObject.toString());
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject)

Example 9 with JsonObject

use of com.google.gson.JsonObject in project weixin-java-tools by chanjarster.

the class WxCpServiceImpl method replaceParty.

@Override
public String replaceParty(String mediaId) throws WxErrorException {
    String url = "https://qyapi.weixin.qq.com/cgi-bin/batch/replaceparty";
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("media_id", mediaId);
    return post(url, jsonObject.toString());
}
Also used : JsonObject(com.google.gson.JsonObject)

Example 10 with JsonObject

use of com.google.gson.JsonObject in project weixin-java-tools by chanjarster.

the class WxMenuGsonAdapter method deserialize.

public WxMenu deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    /*
     * 操蛋的微信
     * 创建菜单时是 { button : ... }
     * 查询菜单时是 { menu : { button : ... } }
     */
    WxMenu menu = new WxMenu();
    JsonObject menuJson = json.getAsJsonObject().get("menu").getAsJsonObject();
    JsonArray buttonsJson = menuJson.get("button").getAsJsonArray();
    for (int i = 0; i < buttonsJson.size(); i++) {
        JsonObject buttonJson = buttonsJson.get(i).getAsJsonObject();
        WxMenu.WxMenuButton button = convertFromJson(buttonJson);
        menu.getButtons().add(button);
        if (buttonJson.get("sub_button") == null || buttonJson.get("sub_button").isJsonNull()) {
            continue;
        }
        JsonArray sub_buttonsJson = buttonJson.get("sub_button").getAsJsonArray();
        for (int j = 0; j < sub_buttonsJson.size(); j++) {
            JsonObject sub_buttonJson = sub_buttonsJson.get(j).getAsJsonObject();
            button.getSubButtons().add(convertFromJson(sub_buttonJson));
        }
    }
    return menu;
}
Also used : JsonArray(com.google.gson.JsonArray) WxMenu(me.chanjar.weixin.common.bean.WxMenu) JsonObject(com.google.gson.JsonObject)

Aggregations

JsonObject (com.google.gson.JsonObject)1124 JsonParser (com.google.gson.JsonParser)263 JsonElement (com.google.gson.JsonElement)228 JsonArray (com.google.gson.JsonArray)226 JsonPrimitive (com.google.gson.JsonPrimitive)78 HashMap (java.util.HashMap)78 Test (org.junit.Test)77 Gson (com.google.gson.Gson)70 Map (java.util.Map)66 ArrayList (java.util.ArrayList)63 Test (org.testng.annotations.Test)61 IOException (java.io.IOException)53 InputStreamReader (java.io.InputStreamReader)27 JsonParseException (com.google.gson.JsonParseException)24 File (java.io.File)21 HttpResponse (org.apache.http.HttpResponse)21 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)19 JsonReader (com.google.gson.stream.JsonReader)17 InputStream (java.io.InputStream)17 URL (java.net.URL)15