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

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 {// 创建最外层的JSONObjectJSONObject 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数组添加到最外层的JSONObjectrootObject.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.mrgr.cn/news/57580.html

相关文章:

  • 【含开题报告+文档+PPT+源码】基于vue框架的东升餐饮点餐管理平台的设计与实现
  • 第二十九篇:TCP的报文格式,TCP系列三
  • 深入解析机器学习算法
  • 构建一个好用的设备管理APP
  • uniapp使用webView打开的网页有缓存如何解决(APP,微信小程序)
  • C++ Mutex
  • 双十一护眼台灯测评推荐:实测书客、柏曼和明基护眼台灯怎么样
  • Lora算法原理及应用
  • qt QLineEdit详解
  • 大模型微调实战:基于 LLaMAFactory 通过 LoRA 微调修改模型自我认知
  • 分组密码填充模式
  • 自闭症学校:儿童成长的崭新希望
  • Spring Boot:植物健康监测的智能管家
  • 请列举四种「等比例自适应矩形」实现方案?
  • API接口在各个领域的发挥着什么样的作用呢
  • 1024程序员日|向改变世界的程序员 致敬!
  • 字符串-04-字符串加解密
  • 最新整理:自动化测试常见面试题
  • fmql之Linux中I2C总线框架
  • 开源模型应用落地-Qwen2-VL-7B-Instruct-vLLM-OpenAI API Client调用
  • 基于RabbitMQ,Redis,Redisson,RocketMQ四种技术实现订单延时关闭功能及其相关优缺点介绍(以12306为主题)
  • Stability.AI 发布 SD3.5 模型,能否逆袭击败 FLUX?如何在ComfyUI中的使用SD3.5?
  • 使用gpt2-medium基座说明模型微调
  • anolis os 8.8 修改kube-proxy的模式为ipvs-kubeadm部署
  • arcgis pro 3.3.1安装教程
  • 重学SpringBoot3-Spring WebFlux之HttpHandler和HttpServer