简体   繁体   English

尝试发送 post rquest 时出现无效的 json 错误

[英]Getting invalid json error when trying to send post rquest

I have a JSON string that I convert to JSON using GSON library down below:我有一个 JSON 字符串,我使用下面的 GSON 库将其转换为 JSON :

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.我不确定我的 JSON 字符串有什么问题,因为它看起来格式正确,后来转换为 JSON。 What would the cause of this error be?这个错误的原因是什么?

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.它旨在获取 Object 实例,并将其转换为 json 字符串格式。

What you are trying to do is pass a JSON string into that method.您要做的是将 JSON 字符串传递给该方法。 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:或者,如果您没有映射到 JSON 的 class,而只是想要一个通用的:

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

JSON parsing using Gson for Java JSON 使用 Gson 解析 Java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Loopj发布方法失败-尝试发送发布JSON时出错 - Loopj Post method failure - error while trying to send post JSON 尝试通过 android 调用 JSON api 时出现无效请求错误 - invalid request error when trying to call a JSON api through android 尝试将邮件发送到动态电子邮件ID时出现错误 - Getting Error when trying to send mail to dynamic Email Id 尝试执行HTTP Post时出现NetworkOnMainThread错误 - Getting NetworkOnMainThread error when trying to do HTTP Post 尝试 POST 到端点时出现未知协议错误 - Getting unknown protocol error when trying to POST to endpoint 尝试将纬度和经度发布到数据库时,Android JSON解析错误 - Android JSON parsing error when trying to post latitude and longitude to database 尝试将Ajax JSON响应存储到变量中时出现未定义错误 - Getting Undefined Error when trying to store ajax JSON response into variable 尝试使用GSON反序列化JSON字符串时出现错误 - Getting an error when trying to deserialize a JSON string with GSON 尝试访问swagger.json时出现404错误 - Getting 404 error when trying to access swagger.json 尝试从WebService获取数据时出现“无效的JSON原语”错误 - Getting “Invalid JSON primitive” error while trying to get data from WebService
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM