您提供的文档不是有效的 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}]
[{"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"}]