-1

I have a JSON string that I convert to JSON using GSON library down below:

Gson g = new Gson();
String json = "{\"user\":\"c8689ls8-7da5-4ac8-8afa-5casdf623302\",\"pass\":\"22Lof7w9-2b8c-45fa-b619-12cf27dj386\"}";
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(g.toJson(json));
out.flush();
out.close();

However when I send the actual post request, I get the following error message:

{"message":"Invalid JSON: Unexpected string literal\n at [Source: UNKNOWN; line: 1, column: 108]","_links":{"self":{"href":"/endpoint","templated":false}}}

Im not sure what is wrong with my JSON string as it looks properly formatted and is later converted to JSON. What would the cause of this error be?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • I think `22Lof7w9-2b8c-45fa-b619-12cf27dj386` has to be in quotes, too. (`"22Lof7w9-2b8c-45fa-b619-12cf27dj386"`) – JCWasmx86 Dec 04 '20 at 15:39
  • @JCWasmx86 I edited code above to have it be in quotes, but I still get the same error – marcus_smithers1994 Dec 04 '20 at 15:49
  • 1
    What is `g`? To get better help post [mcve] (minimal code - without any irrelevant parts, but still complete - so we could copy and paste it to our machines and *without any modifications* run it to get exactly same problem as described in question). – Pshemo Dec 04 '20 at 15:51
  • @Pshemo as mentioned in the description, I convert the string to json using GSON library. g is simply a new instance of GSON. Updated code to make it more clear – marcus_smithers1994 Dec 04 '20 at 15:54
  • `Gson.toJson` looks like attempt to use static method of Gson class which is either incorrect (because there is no such static method) or misleading (because variables are usually named from lowerCase). I edited your example a little, hope it is now closer to your actual code. – Pshemo Dec 04 '20 at 16:06
  • 1
    But could you clarify what are you trying to do here? In question you already have JSON String (which I am assuming your API should send to client), so what are you trying to achieve at `g.toJson(json)`? What do you want `out.writeBytes(..)` to send exactly? – Pshemo Dec 04 '20 at 16:10

1 Answers1

1
This method serializes the specified object into its equivalent Json representation.
   * This method should be used when the specified object is not a generic type. This method uses
   * {@link Class#getClass()} to get the type for the specified object, but the
   * {@code getClass()} loses the generic type information because of the Type Erasure feature
   * of Java. Note that this method works fine if the any of the object fields are of generic type,
   * just the object itself should not be of a generic type. If the object is of generic type, use
   * {@link #toJson(Object, Type)} instead. If you want to write out the object to a
   * {@link Writer}, use {@link #toJson(Object, Appendable)} instead.
   *
   * @param src the object for which Json representation is to be created setting for Gson
   * @return Json representation of {@code src}.
   */
  public String toJson(Object src) {
    if (src == null) {
      return toJson(JsonNull.INSTANCE);
    }
    return toJson(src, src.getClass());
  }

It is meant to take an Object Instance, and convert it to json String format.

What you are trying to do is pass a JSON string into that method. It wont work


What you want is

gson.fromJson("yourStringJSONHere", YourClassWhichMatchesJSONVariables.class)

Or if you dont have a class that maps into your JSON, and simply want a generic one:

JsonElement jelement = new JsonParser().parse(yourJSONString);

JSON parsing using Gson for Java

Pshemo
  • 122,468
  • 25
  • 185
  • 269
JCompetence
  • 6,997
  • 3
  • 19
  • 26