当前位置: 首页> 教育> 大学 > 7位数qq免费申请永久_免费代理服务器ip地址_seo常见的优化技术_cba排名最新排名

7位数qq免费申请永久_免费代理服务器ip地址_seo常见的优化技术_cba排名最新排名

时间:2025/7/10 17:27:24来源:https://blog.csdn.net/dengfuma/article/details/143179874 浏览次数:0次
7位数qq免费申请永久_免费代理服务器ip地址_seo常见的优化技术_cba排名最新排名

        在做网络请求的时候需要生成一个如下的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);}
}
关键字:7位数qq免费申请永久_免费代理服务器ip地址_seo常见的优化技术_cba排名最新排名

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: