gpt4 book ai didi

Json 到 Gson - 建模

转载 作者:行者123 更新时间:2023-12-04 06:23:21 28 4
gpt4 key购买 nike

我正在尝试将 JSON 转换为 GSON ,但我无法建模。谁能给我举个例子。

[
{
"id": "1",
"name": "lalala",
"object1": [
"string1",
"string1",
"string1"
],
"object2": [
"anotherString1",
"anotherString2"
]
},
{
"id": "2",
"name": "laaaaalala",
"object1": [
"string1",
"string1",
"string1"
],
"object2": [
"anotherString1",
"anotherString2"
]
}
]

谢谢

最佳答案

干得好。

import java.io.FileReader;
import java.util.List;

import com.google.gson.Gson;

public class Foo
{
public static void main(String[] args) throws Exception
{
Gson gson = new Gson();
Thing[] things = gson.fromJson(new FileReader("input.json"), Thing[].class);
System.out.println(gson.toJson(things));
}
}

class Thing
{
String id;
String name;
String[] object1;
List<String> object2;
}

关于Json 到 Gson - 建模,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6300920/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com