Gson Throws Malformedjsonexception

gson throws MalformedJsonException

I suspect that result1 has some characters at the end of it that you can't see in the debugger that follow the closing } character. What's the length of result1 versus result2? I'll note that result2 as you've quoted it has 169 characters.

GSON throws that particular error when there's extra characters after the end of the object that aren't whitespace, and it defines whitespace very narrowly (as the JSON spec does) - only \t, \n, \r, and space count as whitespace. In particular, note that trailing NUL (\0) characters do not count as whitespace and will cause this error.

If you can't easily figure out what's causing the extra characters at the end and eliminate them, another option is to tell GSON to parse in lenient mode:

Gson gson = new Gson();
JsonReader reader = new JsonReader(new StringReader(result1));
reader.setLenient(true);
Userinfo userinfo1 = gson.fromJson(reader, Userinfo.class);

com.google.gson.stream.MalformedJsonException in Codemagic

I was facing this issue because the script provided in the Codemagic documentation was only meant to read pure json file but in my case; the the json file was encrypted to Base64 string, so it was necessary to decrypt the binary into a valid json file before parsing it.

Here's the original script from the doc

#!/usr/bin/env sh
set -e # exit on first failed command

echo $ANDROID_FIREBASE_SECRET > $FCI_BUILD_DIR/android/app/google-services.json
echo $IOS_FIREBASE_SECRET > $FCI_BUILD_DIR/ios/Runner/GoogleService-Info.plist

Here's the working script

#!/usr/bin/env sh
set -e # exit on first failed command

echo $ANDROID_FIREBASE_SECRET | base64 --decode > $FCI_BUILD_DIR/android/app/google-services.json
echo $IOS_FIREBASE_SECRET | base64 --decode > $FCI_BUILD_DIR/ios/Runner/GoogleService-Info.plist

Reference: https://blog.codemagic.io/how-to-load-firebase-config-in-codemagic-with-environment-variables/

JsonObject Throws com.google.gson.stream.MalformedJsonException

Are you missing out the API GET method which is actually pulling your response?

String json = ("newsapi.org/v2/top-headlines? country = de & category = business & apiKey = myapikeyhere ");

is currently just a string, as what is returned from that API is a well-formed JSON object

How to resolve MalformedJsonException: Unterminated object that exist even after setting JsonReader to lenient

im not sure why you need the JsonParser and reader in this use case.

i think you can accomplish what you wanted in the following:

byte[] encoded = Files.readAllBytes(Paths.get("<your path>"));
Type mapType = new TypeToken<Map<String, Object>>(){}.getType();
Map<String, Object> firstMap = new Gson().fromJson(new String(encoded, Charset.defaultCharset()), mapType);

and make sure that the json is valid

What is path in GSON MalformedJsonException error message?

JsonReader constructs a MalformedJsonException using JsonPath, see code here.

The Javadoc for getPath() (for getting that JSONPath) states

Returns a JsonPath to the current location in the JSON value.

JSONPath is a tool to navigate JSON and defines a path $.

Since a JSON structure is usually anonymous and doesn't necessarily have a "root member object" JSONPath assumes the abstract name $ assigned to the outer level object.

It's essentially the root object, array, or other JSON value.

Gson MalformedJsonException

Try to change

THIS

[{\"ProfileId\":\"69c02265-abca-4716-8a2f-ac5d642f876a\",\"DisplayName\":\"baman\",\"IsAvailable\":false,\"Image\":null},{\"ProfileId\":\"1f6bdd5b-c4ea-4566-bc56-ee2f58acece8\",\"DisplayName\":\"nayanthara\",\"IsAvailable\":false,\"Image\":null}]

TO

[{"ProfileId":"69c02265-abca-4716-8a2f-ac5d642f876a","DisplayName":"baman","IsAvailable":false,"Image":null},{"ProfileId":"1f6bdd5b-c4ea-4566-bc56-ee2f58acece8","DisplayName":"nayanthara","IsAvailable":false,"Image":null}]

Gson 'fromJson' issue

You can not use " within a String to quote your JSON keys and values. You either have to escape them (like you did in your first example) or you have use single quotes '.

You are effectively trying to do String concatenation without using +.

This looks for the compiler like a list of Strings with variables in between:

"{"key1":{"key11":"192.168.1.158","key12":"WEB"}}"

The compiler would expect something like this:

"{" + key1 + ":{" + key11 + ":" + 192.168.1.158 + "," + key12 + ":" + WEB + "}}";

If you look at the String this way you immediately see the problem. That's why you should either escape the quotes or use single quotes:

"{\"key1\":{\"key11\":\"192.168.1.158\",\"key12\":\"WEB\"}}"
"{'key1':{'key11':'192.168.1.158','key12':'WEB'}}"


Related Topics



Leave a reply



Submit