gpt4 book ai didi

java.lang.Object 到 arraylist

转载 作者:行者123 更新时间:2023-11-30 06:50:26 25 4
gpt4 key购买 nike

有没有办法可以将这种 java.lang.object 解析/转换为 java.lang.Object 的数组或列表:

[
{procedure_name_txt=nameText, procedure_name_ets_id=ID-1234, procedure_ets_desc=description_123},
{procedure_name_txt=deafness, procedure_name_ets_id=ID-99022, procedure_ets_desc=description_31222}
]

已经尝试使用 Gson,但不幸的是对象格式不是 json 形式。

最佳答案

由于多种原因,您提供的文档不是有效的 JSON:

  • 数组元素键不包含在引号 " 中。
  • 数组元素键/值对不使用 : 分隔,而是使用 = 分隔。
  • 字符串值不包含在引号 " 中。

我几乎可以肯定该文档实际上是一个集合/数组的打印精美的 toString 结果,其元素是映射或覆盖了 toString 的 POJO。此类输出决不意味着以编程方式进行解析。然而,Gson 可以解析“tostring”结果,因为它的简单性违反了 JSON 格式语法并且过于宽松。

private static final String LOOKS_LIKE_JSON = "[\n" +
" {procedure_name_txt=nameText, procedure_name_ets_id=ID-1234, procedure_ets_desc=description_123}, \n" +
" {procedure_name_txt=deafness, procedure_name_ets_id=ID-99022, procedure_ets_desc=description_31222}\n" +
"]";

// private static final Type procedureListType = TypeToken.getParameterized(List.class, Procedure.class).getType();
private static final Type procedureListType = new TypeToken<List<Procedure>>() {
}.getType();

// private static final Type procedureArrayType = Procedure[].class;
private static final Type procedureArrayType = new TypeToken<Procedure[]>() {
}.getType();

private static final Type stringToStringMapListType = new TypeToken<List<Map<String, String>>>() {
}.getType();

private static final Type stringToStringMapArrayType = new TypeToken<Map<String, String>[]>() {
}.getType();

private static final Gson gson = new Gson();
private static final JsonParser jsonParser = new JsonParser();

public static void main(final String... args) {
final List<Procedure> procedureList = gson.fromJson(LOOKS_LIKE_JSON, procedureListType);
System.out.println(procedureList);
final Procedure[] procedureArray = gson.fromJson(LOOKS_LIKE_JSON, procedureArrayType);
System.out.println(Arrays.toString(procedureArray));
final List<Map<String, String>> mapList = gson.fromJson(LOOKS_LIKE_JSON, stringToStringMapListType);
System.out.println(mapList);
final Map<String, String>[] mapArray = gson.fromJson(LOOKS_LIKE_JSON, stringToStringMapArrayType);
System.out.println(Arrays.toString(mapArray));
final JsonElement jsonElement = jsonParser.parse(LOOKS_LIKE_JSON);
System.out.println(jsonElement);
}

其中Procedure类如下:

final class Procedure {

@SerializedName("procedure_name_txt")
final String name = null;

@SerializedName("procedure_name_ets_id")
final String id = null;

@SerializedName("procedure_ets_desc")
final String description = null;

@Override
public String toString() {
return new StringBuilder("Procedure{")
.append("name='").append(name).append('\'')
.append(", id='").append(id).append('\'')
.append(", description='").append(description).append('\'')
.append('}')
.toString();
}

}

输出:

  • IntelliJ IDEA 生成的 Procedure.toString:

[Procedure{name='nameText', id='ID-1234', description='description_123'}, Procedure{name='deafness', id='ID-99022', description='description_31222'}]

  • 字符串到字符串条目的映射列表(实际上这反射(reflect)了给定的输入),JDK toString():

[{procedure_name_txt=nameText, procedure_name_ets_id=ID-1234, procedure_ets_desc=description_123}, {procedure_name_txt=deafness, procedure_name_ets_id=ID-99022, procedure_ets_desc=description_31222}]

  • Gson JSON 树模型toString():

[{"procedure_name_txt":"nameText","procedure_name_ets_id":"ID-1234","procedure_ets_desc":"description_123"},{"procedure_name_txt":"deafness","procedure_name_ets_id":"ID-99022","procedure_ets_desc":"description_31222"}]

关于java.lang.Object 到 arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42918163/

25 4 0