当前位置: 首页 > 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.lryc.cn/news/469037.html

相关文章:

  • C++新增的类功能和可变参数模板
  • redo log 日志 与 undo log 日志工作原理
  • go语言结构体与json数据相互转换
  • jenkins 自动化部署Springboot 项目
  • 使用xml发送国际短信(smspro)【吉尔吉斯斯坦】
  • springmvc-springsecurity-redhat keycloak SAML2 xml实现
  • 【K8S系列】Kubernetes Pod节点CrashLoopBackOff 状态及解决方案详解【已解决】
  • Linux: Shell编程入门
  • python爬虫实战案例——抓取B站视频,不同清晰度抓取,实现音视频合并,超详细!(内含完整代码)
  • 容灾与云计算概念
  • 基于 Python 的自然语言处理系列(44):Summarization(文本摘要)
  • RabbitMQ安装部署
  • 智联招聘×Milvus:向量召回技术提升招聘匹配效率
  • unplugin-auto-import 库作用
  • 【Multisim14.0正弦波>方波>三角波】2022-6-8
  • vue3纯前端验证码示例
  • 招聘程序员
  • Android 判断手机放置的方向
  • Telegram机器人的手机部署
  • ffmpeg视频滤镜: 色温- colortemperature
  • Django+Vue全栈开发项目入门(二)
  • 【ubuntu改源】
  • SQLI LABS | Less-9 GET-Blind-Time based-Single Quotes
  • 【小白学机器学习24】 用例子来比较:无偏估计和有偏估计
  • C++在实际项目中的应用第二节:C++与网络编程
  • 依赖关系是危险的
  • ipguard与Ping32如何加密数据防止泄露?让企业信息更安全
  • gitlab 的备份与回复
  • 创建型模式-----建造者模式
  • 威胁 Windows 和 Linux 系统的新型跨平台勒索软件:Cicada3301