JSON错误“java.lang.IllegalStateException:期望的BEGIN_OBJECT,但在第1行第1列路径$”STRING“ [英] JSON Error "java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $"

查看:1715
本文介绍了JSON错误“java.lang.IllegalStateException:期望的BEGIN_OBJECT,但在第1行第1列路径$”STRING“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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);

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

这个创建异常

  com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期的BEGIN_OBJECT,但是在第1行第1列路径中为STRING $ 

非常感谢您的帮助。

解决方案

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


期望的BEGIN_OBJECT


您的JSON是一个对象,并且所有JSON对象都用大括号({})括起来。 BEGIN_OBJECT因此是{。


但是是STRING



$ b $但是他却发现了一个字符串Something。还没有告诉我们在哪里。


at line 1 column 1 path $


啊,完美。在第1行第1列。哪个是JSON的开始。所以你忘了把所有东西都放在{}中(或者至少你已经忘记了第一个,但我敢打赌你们都忘记了它们)。

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.

解决方案

Let's look at the error you are receiving.

Expected 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.

but was STRING

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

at line 1 column 1 path $

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).

这篇关于JSON错误“java.lang.IllegalStateException:期望的BEGIN_OBJECT,但在第1行第1列路径$”STRING“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆