【问题标题】:java.lang.Object to arraylistjava.lang.Object 到数组列表
【发布时间】:2017-08-12 14:10:39
【问题描述】:

有没有办法可以将这种 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 格式。

【问题讨论】:

  • @HovercraftFullOfEels 不是 JSON:键/值上没有双引号;和 = 而不是:
  • @john 谢谢,评论已删除

标签: java json type-conversion gson


【解决方案1】:

您提供的文档不是有效的 JSON,原因如下:

  • 数组元素键没有用引号括起来"
  • 数组元素键/值对不是用:分隔,而是用=分隔。
  • 字符串值没有括在引号"

我几乎可以肯定该文档实际上是一个漂亮打印的 toString 集合/数组的结果,其元素是地图或 POJO,并覆盖了 toString。此类输出从不意味着以编程方式解析。但是,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

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

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

[{procedure_name_txt=nameText, procedure_name_ets_id=ID-1234, procedure_ets_desc=description_123}, {procedure_name_txt=耳聋, 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":"耳聋","procedure_name_ets_id":"ID-99022" ,"procedure_ets_desc":"description_31222"}]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    • 2011-12-19
    • 2020-09-14
    • 1970-01-01
    • 2012-05-07
    相关资源
    最近更新 更多