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);
}
}