当前位置: 首页 > 知识库问答 >
问题:

JDA:如何从JSON文件创建MessageEmbed?

充普松
2023-03-14

我想在一条消息中发送两个嵌入。这应该可以通过MessageChannel#sendmagesembeds(java.util.Collection)实现
此外,我还嵌入了如下JSON文件

{
    "author": {
      "name": "I use this as title because why not",
      "icon_url": "url for icon"
    },
    "description": "description that goes top of the embed",
    "color": 65535,
    "fields": [
      {
        "name": "field name 01",
        "value": "field value 01 which *can* ||have|| __some__ markdown stuff",
        "inline": false
      },
      {
        "name": "field name 02",
        "value": "yes, thie fileds can have more than one filed",
        "inline": false
      },
      {
        "name": "field name 03",
        "value": "blahblah some texts",
        "inline": true
      },
    ],
    "thumbnail": {
      "url": "url for thumbnail"
    },
    "footer": {
      "text": "footer text because it is cool",
      "icon_url": "this would be same as author icon_url but this may vary sometimes"
    }
  }

问题是,我想不起“如何将JSON文件转换为MessageEmbedobject”。我在JDA discord服务器中询问,在谷歌上搜索一些提示,但仍然无法想出正确的方法。

长话短说,我有一个用于嵌入的JSON文件,我想将JSON文件转换为MessageEmbed对象,以便将其发送到Discord频道。

共有1个答案

靳彦
2023-03-14

也许有一种内置的方法可以将JSON转换为MessageEmbeddes,如果是这样的话,我浪费了时间,我自己也找不到。

下面是一个方法,它接受一个JsonObject,并将其有效成员转换为MessageEmbed,其中包含您在Json和标题中列出的所有类别,因为我注意到您没有添加它。如果您想添加更多,欢迎使用它并这样做。

注意:我使用谷歌的GSON库来实现这一点,如果你想要我使用的方法,你必须将它添加到你的项目中。欲了解更多信息,请参见此处

方法代码:

/**
 * Converts a {@link JsonObject} to {@link MessageEmbed}.
 * Supported Fields: Title, Author, Description, Color, Fields, Thumbnail, Footer.
 * 
 * @param json The JsonObject
 * @return The Embed
 */
public static MessageEmbed jsonToEmbed(JsonObject json){
    EmbedBuilder embedBuilder = new EmbedBuilder();

    JsonPrimitive titleObj = json.getAsJsonPrimitive("title");
    if (titleObj != null){ // Make sure the object is not null before adding it onto the embed.
        embedBuilder.setTitle(titleObj.getAsString());
    }

    JsonObject authorObj = json.getAsJsonObject("author");
    if (authorObj != null) {
        String authorName = authorObj.get("name").getAsString();
        String authorIconUrl = authorObj.get("icon_url").getAsString();
        if (authorIconUrl != null) // Make sure the icon_url is not null before adding it onto the embed. If its null then add just the author's name.
            embedBuilder.setAuthor(authorName, authorIconUrl);
        else
            embedBuilder.setAuthor(authorName);
    }

    JsonPrimitive descObj = json.getAsJsonPrimitive("description");
    if (descObj != null){
        embedBuilder.setDescription(descObj.getAsString());
    }

    JsonPrimitive colorObj = json.getAsJsonPrimitive("color");
    if (colorObj != null){
        Color color = new Color(colorObj.getAsInt());
        embedBuilder.setColor(color);
    }

    JsonArray fieldsArray = json.getAsJsonArray("fields");
    if (fieldsArray != null) {
        // Loop over the fields array and add each one by order to the embed.
        fieldsArray.forEach(ele -> {
            String name = ele.getAsJsonObject().get("name").getAsString();
            String content = ele.getAsJsonObject().get("value").getAsString();
            boolean inline = ele.getAsJsonObject().get("inline").getAsBoolean();
            embedBuilder.addField(name, content, inline);
        });
    }

    JsonPrimitive thumbnailObj = json.getAsJsonPrimitive("thumbnail");
    if (thumbnailObj != null){
        embedBuilder.setThumbnail(thumbnailObj.getAsString());
    }

    JsonObject footerObj = json.getAsJsonObject("footer");
    if (footerObj != null){
        String content = footerObj.get("text").getAsString();
        String footerIconUrl = footerObj.get("icon_url").getAsString();

        if (footerIconUrl != null)
            embedBuilder.setFooter(content, footerIconUrl);
        else
            embedBuilder.setFooter(content);
    }

    return embedBuilder.build();
}

如何使用:

textChannel.sendMessageEmbeds(jsonToEmbed((JsonObject) JsonParser.parseReader(new FileReader("jsonfile.json")))).queue();

 类似资料:
  • 问题内容: 我想知道如何从json文件或xml文件创建对象类? 例如: 我从webservice获取此json文件: 我想创建一个像这样的类: NB : 我的问题不是如何在vb.net 中序列化/反序列化json objet 吗? 我的xml文件没有XSD,为什么会更困难 我的代码是用VB.Net而不是C#编写的。我发现许多网站将json转换为c#(http://json2csharp.com/)

  • 我正在处理非常长的嵌套JSON文件中的数据。问题是,这些文件的结构并不总是相同的,因为有些文件缺少其他文件的列。我想从一个包含所有列的空JSON文件创建一个定制模式。如果我稍后将JSON文件读入这个预定义的模式,不存在的列将被空值填充(至少计划是这样的)。到目前为止我所做的: 将测试 JSON(不包含预期的所有列)加载到数据帧中 将其架构写入 JSON 文件 在文本编辑器中打开此 JSON 文件并

  • 我正在构建一个创建XML文件的简单java程序。我正在构建以下代码: 但如果我尝试启动此代码,我会出现以下错误: 线程“AWT-EventQueue-0”java中出现异常。lang.AbstractMethodError:gnu。xml。dom。DomDocument。com上的getXmlStandalone()Z。太阳组织。阿帕奇。泽兰。内部的xsltc。特拉克斯。DOM2TO。com上的s

  • 问题内容: 我有一个相当最新的android studio版本,我在一个基本上应该是库的项目下创建了一个模块,在构建它时,它会创建一个“ .aar”文件,我想要的是.jar文件,因为库也应该与Eclipse一起使用。 该库还包含活动,是否可以通过任何方式创建可以在Eclipse和Android Studio上使用的.jar文件? 我已经创建了这个模块并尝试构建,因此它生成了.aar文件而不是.ja