I’m using GSON to convert a JSON string stored in our DB to our corresponding object class.
String configData = dbCall.fetchPublishedData(dbURL); //returns JSON string
Gson gson = new Gson();
publishDataObject = gson.fromJson(configData, PublishedDataVO.class);
For the most part, this is working, except for one particular row in our DB that is failing with an exception from the GSON parser. All of our rows in the DB are created with the same tool and have the same data structure, the only difference for this one row is that it holds A LOT of data in its JSON string. When I try to retrieve this data and transform it into our class object, I am getting the following error:
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated string at line 1 column 4001 path...
The size of the data string in this row is the only difference with the other row's strings. I even took the JSON string and formatted it to see if it is a valid JSON string, and it doesn't show errors. Does GSON have a limit on the input string size for its fromJSON method?
Related
I am getting a JSON in below format:
{
"A":"1",
"B":"2"
}
I have a field update and JSON at some point could be too long.
How can I change the JSON format to below pattern?
{"A":"1","B":"2"}
I am trying to store this minified JSON format on a field, so that char limit issue is resolved.
deserialize the string and serialize it again and then update the field.
Declare the JSON as string
Map<String,Object> parser = (Map<String,Object>)JSON.deserializeUntyped(yourstring);
yourstring = JSON.serialize(parser);
You can change Map accordingly. Required format will be present in string variable.
I'm upgrading some legacy Android / Java code that pulls in data from a server. The legacy code uses HttpURLConnection and pulls in a inputStream and converts it to a GZipInputStream. The result is then converted to a JSONObject. Then the data is parsed manually. This all works fine, meaning valid json is returned as it starts with '{'
I'm trying to updgrade this code using Retrofit2 and the GsonConverterFactory so I can auto map the json to Classes.
I'm getting an error: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
This tells me that the data is coming in as a string and not a JSON object.
NOTE: I'm sending the same headers and query parameters for both my legacy code and new Retrofit implementation --- but different result -- can't figure out why.
The issue was that the server was expecting all of my query parameters to be in one field and the field named "Object" and in a json format i.e: Object={"field1":"fieldOneValue","field2":"fieldTwoValue"} AND had to be Url Uncoded Format:
#FormUrlEncoded
#POST("testing/myapi.api")
Observable<MyClass> getMyApiData(#Url String url, #Field("Object") String object);
QUESTION: Can you use CsvMapper/CsvSchema to convert csv to a map<string, object> where the object is something other than string, like boolean or integer?
Details: I have some csv files that I want to convert directly to json. I don't have access to the POJO's. But I do have an xml schema file that contains the type information for the columns in the csv data.
After doing some searching I decided to use Jackson's CsvMapper/CsvSchema to convert the csv files to json. I created a CsvSchema with column type information then converted the csv files to a Map<> file then converted the map file to json. It all worked fine except for one thing. All the json properties were Strings, even the properties that the CsvSchema had defined as Boolean and Numeric types. Below is a small snippet that shows what I'm doing.
String csvEmployeeData="\"Bob\",25,true\n" +
"\"Joe\",35,false\n" +
"\"Tom\",45,false\n";
CsvSchema schema = CsvSchema.builder().addColumn("name", ColumnType.String)
.addColumn("age", ColumnType.NUMBER)
.addColumn("isManager", ColumnType.BOOLEAN)
.build();
CsvMapper csvMapper = new CsvMapper();
MappingIterator<Map<String, ? extends Object>> it = csvMapper.readerFor(Map.class)
.with(schema)
.readValues(csvEmployeeData);
List<Map<String, ? extends Object>> objectList=it.readAll();
Map<String, ? extends Object> employeeObj=objectList.get(0);
assertEquals("java.lang.Integer", employeeObj.get("age").getClass().getTypeName());
// Integer age=(Integer)employeeObj.get("age");
// java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
I'm reading the csv data in to a Map where the string is the property name and the Object is value. I thought the value type for Object would be based on the Schema ColumnType. I.e. if it was a ColumnType.BOOLEAN then Object would be Boolean. However all the Objects in the Map are String types.
I was assuming that the CsvSchema ColumnType's would be used in the conversion since in csv everything is a string so you don't need the schema for that. I'm clearly missing something. Surprisingly Google doesn't bring up a lot on using CsvMapper.
Does anyone know if possible to go from CSV to json and use the json types (bool/integer) or does everything have to be a string?
I'm trying to convert an xml to json data using the java-json.jar . The conversion is done. but have issues with the resulting JSON data like
An Integer is expected but the data is converted as String.
it would not create a list if there was only one child element ( even when it should be a list as per the JSON schema ).
Is there any way to convert XML into JSON based on the JSON schema in Java?
I'm having trouble parsing a json response. Its created via php and sent back to my phone via http as json.
It is an array with 3 arrays in it so...
$arr = array();
Then I search my mysql database for specific IDs relating to the query, usually about 3 results are returned with unique ids as the array index.. like this
$sql = mysql_query("Select from so and so");
while($row = mysql_fetch_assoc($sql)){
$arr[$row["ID"]] = $row;
}
print(json_encode($arr));
so now in my android (java) code I'm trying to convert the response to a json object and then parse it with
json_object.getString("FirstName")
for all 3 of the first names returned but its crashing. So i am guess I need to parse out the 3 individual arrays first which is where I am stuck.
-The question is how do I sort out the arrays returned within this one object. Each of them have the same keys, but different values
-crashing wasnt a good choice in terms, what I should have said is it cant find the value I am searching for when I use the getString method, here is the return
Agree with the comments above, would need more details.
But if you're getting back a properly formatted JSON response and it's an Array, you could always do something like ...
JSONArray results = new JSONArray(<json response string>);
for (int i=0; i<results.length(); i++) {
JSONObject obj = results.getJSONObject(i);
}
And documentation for your reference:
http://developer.android.com/reference/org/json/JSONArray.html