Context :
I am trying to convert the following JSON into a JAVA POJO using gson
JSON :
{
"task": "findRecords",
"foundRecords": "[1234567, 11234512]",
"logs": "records found [1234567, 11234512]",
"status": "success"
}
POJO Class :
public class JavaPojo {
String task = null;
JsonArray foundRecords = null;
String logs = null;
String status = null;
}
Conversion logic :
JavaPojo pojo = gson.fromJson(jsonString, JavaPojo.class);
where jsonString
is the string representation of the above mentioned json.
Problem :
The conversion logic fails with
com.google.gson.JsonSyntaxException: Expected a com.google.gson.JsonArray but was com.google.gson.JsonPrimitive
From what i understand gson is interpreting foundRecords
's value as purely string and is not parsing it into an Array.
Now to get around this i modified foundRecords
from JsonArray
to String
. the conversion happened successfully.
Any clue how can i achieve the conversion while preserving the type of foundRecords
as JsonArray
?
Note:
After converting foundRecords
to String
, i tried converting the string value of foundRecords
to JsonArray
using gson and it strangely worked.
//This works
//Map json to pojo
JavaPojo pojo = gson.fromJson(jsonString, JavaPojo.class);
//Convert foundRecords into a JsonArray
JsonArray arr = gson.fromJson(pojo.getFoundRecords(), JsonArray.class);
So now i am confused, if gson can convert string
type foundRecords
into JsonArray
then why couldn't it perform the conversion earlier ?
I understand the REST API response is bad and is violating the JSON syntax. Solution is to correct the REST API, but in my scenario unfotunately i cannot request for API correction so i wrote a util at my end to clean the jsonString
.
Posting it here if it helps anyone :
/**
* @param malformedArrayKey
* - Name of the key in the JSON object that has a malformed array
* for e.g consider following JSON object having a bad formed array
* <pre>
* {
* "task": "findRecords",
* "foundRecords": "[1234567, 11234512]",
* }
* </pre>
* @param jsonString
* - String representation of the JSON object containing the malformed array
* @return - json string having well formed array against the key {@code malformedArrayKey} supplied
* <pre>
* {
* "task": "findRecords",
* "foundRecords": [1234567, 11234512]
* }
* </pre>
*/
public static String formatMalformedArray(String malformedArrayKey, String jsonString) {
JsonObject jsonObj = gson.fromJson(jsonString, JsonObject.class);
// get the faulty key value
String malformedArrayKeyValue = jsonObj.get(malformedArrayKey)
.getAsString();
// drop it
jsonObj.remove(malformedArrayKey);
// create a array out of the malformed array string
JsonArray jsonArray = gson.fromJson(malformedArrayKeyValue, JsonArray.class);
// add the array back to the object
jsonObj.add(malformedArrayKey, jsonArray);
// now convert it into a well formed json string
return jsonObj.toString();
}
The method is quite basic but it satisifes my use case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.