java.lang.Object到arraylist

时间:2017-03-21 03:55:53

标签: java json type-conversion gson

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

[
    {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格式。

1 个答案:

答案 0 :(得分: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 =&#39; nameText&#39;,id =&#39; ID-1234&#39;,description =&#39; description_123&#39;},程序{name =&#39;耳聋&#39;,id =&#39; ID-99022&#39;,description =&#39; description_31222&#39;}]

  • 字符串到字符串条目的映射列表(实际上这反映了给定的输入),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()
  

[{&#34; procedure_name_txt&#34;:&#34; nameText&#34;&#34; procedure_name_ets_id&#34;:&#34; ID-1234&#34;&#34; procedure_ets_desc&#34 ;:&#34; description_123&#34;},{&#34; procedure_name_txt&#34;:&#34;耳聋&#34;&#34; procedure_name_ets_id&#34;:&#34; ID-99022&#34; ,&#34; procedure_ets_desc&#34;:&#34; description_31222&#34;}]