有没有办法可以将这种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格式。
答案 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();
}
}
输出:
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;}]
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}]
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;}]