当前位置: 首页 > article >正文

android 生成json 文件

        在做网络请求的时候需要生成一个如下的json文件:

{
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "image_base64",
                    "image_base64": "pp"
                },
                {
                    "type": "text",
                    "text": "msg"
                }
            ]
        }
    ],
    "model": "5-Vision",
    "temperature": 0.8,
    "max_new_tokens": 512,
    "top_p": 0.35,
    "repetition_penalty": 1.25,
    "stream": true,
    "user": "test"
}

1. org.json 生成

        以下是使用org.json库来手动构建这个JSON对象的示例代码:

import org.json.JSONArray;
import org.json.JSONObject;

public class JsonExample {
    public static void main(String[] args) {
        try {
            // 创建最外层的JSONObject
            JSONObject rootObject = new JSONObject();

            // 创建messages数组
            JSONArray messagesArray = new JSONArray();

            // 创建第一个message对象
            JSONObject messageObject = new JSONObject();
            messageObject.put("role", "user");

            // 创建content数组
            JSONArray contentArray = new JSONArray();

            // 创建image_base64对象
            JSONObject imageBaseObject = new JSONObject();
            imageBaseObject.put("type", "image_base64");
            imageBaseObject.put("image_base64", "pp");
            contentArray.put(imageBaseObject);

            // 创建text对象
            JSONObject textObject = new JSONObject();
            textObject.put("type", "text");
            textObject.put("text", "msg");
            contentArray.put(textObject);

            // 将content数组添加到message对象
            messageObject.put("content", contentArray);

            // 将message对象添加到messages数组
            messagesArray.put(messageObject);

            // 将messages数组添加到最外层的JSONObject
            rootObject.put("messages", messagesArray);

            // 添加其他字段
            rootObject.put("model", "5-Vision");
            rootObject.put("temperature", 0.5);
            rootObject.put("max_new_tokens", 512);
            rootObject.put("top_p", 0.35); // 这里去掉了空格
            rootObject.put("repetition_penalty", 1.25);
            rootObject.put("stream", true);
            rootObject.put("user", "test");

            // 打印JSON字符串
            String jsonString = rootObject.toString(4); // 4是缩进空格数
            System.out.println(jsonString);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2. Gson生成

        Gson在Android中生成你提供的JSON结构,你首先需要定义一些Java类来表示JSON中的对象。然后,你可以使用Gson将这些对象序列化为JSON字符串。在Java类中可以使用@SerializedName注解别名。另外还需要在gradle中引入Gson库;

build.gradle中添加Gson

implementation 'com.google.code.gson:gson:2.10.1'

Java 代码如下:

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;

// 定义Content类
class Content {
    String type;
    String image_base64;
    String text;

    Content(String type, String image_base64, String text) {
        this.type = type;
        this.image_base64 = image_base64;
        this.text = text;
    }
}

// 定义Message类
class Message {
    String role;
    List<Content> content;

    Message(String role, List<Content> content) {
        this.role = role;
        this.content = content;
    }
}


class RequestBody {
    List<Message> messages;
    String model;
    double temperature;
    int max_new_tokens;
    @SerializedName("top_p")
    double top_p;
    double repetition_penalty;
    boolean stream;
    String user;

    RequestBody(List<Message> messages, String model, double temperature, int max_new_tokens, double top_p, double repetition_penalty, boolean stream, String user) {
        this.messages = messages;
        this.model = model;
        this.temperature = temperature;
        this.max_new_tokens = max_new_tokens;
        this.top_p = top_p;
        this.repetition_penalty = repetition_penalty;
        this.stream = stream;
        this.user = user;
    }
}

public class JsonExample {
    public static void main(String[] args) {
        // 创建Content对象列表
        List<Content> contents = new ArrayList<>();
        contents.add(new Content("image_base64", "pp", null));
        contents.add(new Content("text", null, "msg"));

        // 创建Message对象
        Message message = new Message("user", contents);

        // 创建RequestBody对象
        RequestBody requestBody = new RequestBody(
                new ArrayList<Message>() {{ add(message); }},
                "5-Vision",
                0.5,
                1024,
                0.35,
                1.25,
                true,
                "test"
        );

        // 使用Gson序列化RequestBody对象为JSON字符串
        Gson gson = new Gson();
        String json = gson.toJson(requestBody);

        // 打印JSON字符串
        System.out.println(json);
    }
}

http://www.kler.cn/news/366662.html

相关文章:

  • AutoSar AP CM服务接口级别的数据类型总结
  • sheng的学习笔记-AI基础-正确率/召回率/F1指标/ROC曲线
  • 构建中小企业设备管理平台:Spring Boot应用
  • 水轮发电机油压自动化控制系统解决方案介绍
  • 【SQL 数据库索引优化之 覆盖索引】原理深究剖析,拒绝假大空(VIP专属)
  • Lua 语言中的注释详解
  • row_number() over (partition by 分组列 order by 排序列 desc)、row_number() 函数、分组排序函数
  • 计算机网络(十二) —— 高级IO
  • 12_Linux进程管理命令详解
  • python如何通过json以及pickle读写保存数据
  • gin入门教程(9):路由分组与路由版本控制
  • MySQL 存储结构
  • 基于信号分解和多种深度学习结合的上证指数预测模型
  • 基于Multisim的音频放大电路设计与仿真
  • 软体机器人纤维:材料选择有讲究,热拉伸工艺来制造,多种功能应用
  • Spring Boot 配置文件(yml、properties | bootstrap、application)加载顺序
  • Linux基础命令(三)之 重定向操作符,管道符|,tee
  • 1. 路由定义
  • redis高级篇之skiplist跳表 第164节答疑
  • [网络协议篇] TCP协议一
  • git 安装
  • 导出BERT句子模型为ONNX并推理
  • axios直接上传binary
  • PHP 正则表达式 修正符【m s x e ? (?i)】内部修正符 贪婪模式 后向引用 断言【总结篇】
  • 【C++初阶】一文讲通C++内存管理
  • 力扣第 420 场周赛 3324. 出现在屏幕上的字符串序列