The document you've provided is not a valid JSON for several reasons:
- The array element keys are not enclosed in quotes
".
- The array element key/value pairs are not delimited with
:, but are delimited =.
- The string values are not enclosed in quotes
".
I'm almost sure the document is actually a pretty-printed toString result of a collection/array whose elements are either maps or POJOs with toString overridden. Such output is never meant to be parsed programmatically. However, Gson, can parse the "tostring" result due its simplicity violating the JSON format grammar and being tooooo lenient.
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);
}
Where the Procedure class is as follows:
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();
}
}
Output:
- IntelliJ IDEA-generated
Procedure.toString:
[Procedure{name='nameText', id='ID-1234', description='description_123'}, Procedure{name='deafness', id='ID-99022', description='description_31222'}]
- list of maps of string to string entries (actually this reflects the given input), 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 tree model
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"}]