JSON 错误“java.lang.IllegalStateException:预期为 BEGIN_OBJECT 但在第 1 行第 1 列路径 $ 处为 STRING”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31424372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:06:38  来源:igfitidea点击:

JSON Error "java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $"

javaandroidjsongson

提问by androidAnonDev

public interface UserService {
    @POST(Constants.Api.URL_REGISTRATION)
    @FormUrlEncoded
    BaseWrapper registerUser(@Field("first_name") String firstname, @Field("last_name") String lastname, @Field("regNumber") String phone, @Field("regRole") int role);


 public BaseWrapper registerUser(User user) {
        return getUserService().registerUser(user.getFirstName(), user.getLastName(), user.getPhone(), user.getRole());
    }

This create Exception

这创建异常

 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

Big thanks for help.

非常感谢您的帮助。

采纳答案by Astrogat

Let's look at the error you are receiving.

让我们看看您收到的错误。

Expected BEGIN_OBJECT

预期 BEGIN_OBJECT

Your JSON is an object, and all JSON objects are enclosed in curly braces ({}). BEGIN_OBJECT is therefore {. And it's expecting it somewhere.

您的 JSON 是一个对象,所有 JSON 对象都包含在花括号 ({}) 中。因此 BEGIN_OBJECT 是 {。它正在某处期待它。

but was STRING

但是是STRING

But instead he found a string "Something". Still doesn't tell us where.

但相反,他发现了一个字符串“Something”。仍然没有告诉我们在哪里。

at line 1 column 1 path $

在第 1 行第 1 列路径 $

Ah, perfect. At line 1 column 1. Which is the start of the JSON. So you have forgotten to enclose the whole thing in {} (or at least you have forgotten the first one, but I bet you've forgotten them both).

啊,完美。在第 1 行第 1 列。这是 JSON 的开始。因此,您忘记将整个内容包含在 {} 中(或者至少您忘记了第一个,但我敢打赌您已经忘记了它们)。

回答by Kulasekar

Clean and Rebuildthe project This is caused due to some mess over the Gson file.

清理并重建项目 这是由于 Gson 文件有些混乱造成的。

回答by MUHAMMAD UMER

Cleaning and rebuilding the project works for me.

清理和重建项目对我有用。